working on it ...

Filters

Explore Public Snippets

Sort by

Found 77k snippets matching: python

    public by lbottaro modified Feb 13, 2014  1449  9  7  1

    How to check if a Git remote repository is available in python

    This simple python code shows how to check if a given Git remote repository is available. The code invokes the git command 'git ls-remote' on the remote URL path as subprocess with Popen and checks the exit code, using the communicate() method. The command exit status is stored into the 'returncode' property. A value not-equals to '0' mean
    from subprocess import Popen, PIPE, STDOUT
    
    p = Popen(["git", "ls-remote", "git://remote_path"], stdout=PIPE)
    output = p.communicate()[0]
    if p.returncode != 0:
      print "Command failed ["+str(p.returncode)+"]"        

    public by lbottaro modified Oct 30, 2013  1490  11  7  2

    How to create a simple GUI in python using PyQt4

    In this example, we create a simple window using PyQt4 and its QtGui module. Thanks to http://zetcode.com/gui/pyqt4/firstprograms/
    #!/usr/bin/python
    import sys
    from PyQt4 import QtGui
    
    def main():
        # Every PyQt4 application must create an application object.
        # The application object is located in the QtGui module.
        app = QtGui.QApplication(sys.argv)
    
       # The QWidget widget is the base class of all user interface objects in PyQt4.
       # We provide the default constructor for QWidget. The default constructor has no parent.
        # A widget with no parent is called a window.
        w = QtGui.QWidget()
    
        # The resize() method resizes the widget.
        w.resize(250, 150)
        w.move(300, 300)
    
        # Here we set the title for our window.
        w.setWindowTitle('Simple')
    
        # The show() method displays the widget on the screen.
        w.show()
        
        # Finally, we enter the mainloop of the application.
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()

    public by lbottaro modified Mar 18, 2014  2582  40  7  0

    Using lambda function to pass extra argument to slots using PyQt4 python

    When programming with PyQt is useful to pass extra arguments to slots callback methods, in order to execute some code when a UI control's action is invoked. In this example you can see the usage of lambda expression to pass the extra argument to the method invoked when pressing a button. Thanks to http://eli.thegreenplace.net/2011/04/25/passi
    self.greeting = QLabel('', self)
    
    # Create the build button with its caption
    self.build_button = QPushButton('&Build Greeting', self)
     
    # Connect the button's clicked signal to AddControl
    self.build_button.clicked.connect(lambda: self.AddControl('fooData'))
    
    def AddControl(self, name):
        self.greeting.setText('%s!' % (name))
    
    

    public by lbottaro modified Oct 30, 2013  1376  27  6  0

    How to dynamically add a button to a GUI mask in python using PyQt4

    This code shows how to add some dynamic GUI controls (in this case QPushButton) to a GUI main window. In this example when user clicks on a button, the new buttons are created and put into the main window.
    #!/usr/bin/python
    import sys
    from PyQt4 import QtGui
    import os
    
    class Test(QtGui.QMainWindow):  
        def __init__(self):
            super(Test, self).__init__()
    
            self.initUI()
    
        def initUI(self):
            YDrive = QtGui.QAction("Y", self)
            YDrive.triggered.connect(self.setYDir)
    
            self.toolbar = self.addToolBar('Exit')
            self.toolbar.addAction(YDrive)
    
            self.setGeometry(300, 300, 250, 150)
            self.setWindowTitle('Message box')    
            self.show()
    
        def buttonClicked(self):
            print "clicked"
    
        def setYDir(self):
            myWorkDrive = "/tmp"
            shows = os.listdir(myWorkDrive)
            i = 0
            for elements in shows:
                btn1 = QtGui.QPushButton(elements, self)
                btn1.move(30, 50 + i)
                i += 30
                btn1.clicked.connect(self.buttonClicked)
                btn1.show()
            self.resize(self.width(), 50 + i)
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication([])
        t = Test()
        t.show()
        app.exec_()

    public by lbottaro modified Oct 30, 2013  1644  19  7  0

    How to create a simple button in python using GUI PyQt4

    Using PyQt4 python module, you can easily add a QtGui.QPushButton as interactive button in your mask. Constructor QPushButton(string text, QWidget parent = None). The 'text' parameter is the text displayed on the button. The 'parent' is a widget, onto which we place the button. Thanks to http://zetcode.com/gui/pyqt4/firstprograms/
    #!/usr/bin/python
    
    import sys
    from PyQt4 import QtGui, QtCore
    
    class GUI_Example(QtGui.QWidget):
        
        def __init__(self):
            super(GUI_Example, self).__init__()
            self.initUI()
            
        def initUI(self):               
            
            qbtn = QtGui.QPushButton('Quit', self)
            qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
            qbtn.resize(qbtn.sizeHint())
            qbtn.move(55, 55)       
            
            self.setGeometry(300, 300, 250, 150)
            self.setWindowTitle('Quit button')    
            self.show()
            
    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = GUI_Example()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

    public by lbottaro @ Amazon_AWS_Python_API modified Aug 4, 2013  1818  13  7  0

    Amazon S3 Aws - How to get content information for a given bucket using boto python api

    This snippet shows how to iterate through your own Amazon S3 buckets and list the available content.
    import boto
    import boto.s3.connection
    access_key = '<aws access key>'
    secret_key = '<aws secret key>'
    
    # This code initialize the Amazon S3 connection to your account
    conn = boto.connect_s3(
            aws_access_key_id = access_key,
            aws_secret_access_key = secret_key)
    
    # Iteration through all available buckets stored into S3
    # Name and creation date are listed
    for bucket in conn.get_all_buckets():
            print "{name}\t{created}".format(
                    name = bucket.name,
                    created = bucket.creation_date,
            )
            # For each bucket get all data content put into the bucket
            for key in bucket.list():
                    # Get name, size and date
                    print "{name}\t{size}\t{modified}".format(
                            name = key.name,
                            size = key.size,
                            modified = key.last_modified,
                            )

    public by fabio.nosenzo @ Amazon_AWS_Python_API modified Aug 1, 2013  2008  5  7  0

    Amazon S3 Aws - S3Connection object from python boto API

    This snippet is about the S3Connection object provided by python boto package for Amazon AWS API. Here the __init__ method is listed with all its variables. The AWSAuthConnection object is initialized and it handles all the data for managing requests and response from Amazon AWS service. Path: [boto/s3/connection.py] Learn more about S3Con
    import boto.utils
    from boto.connection import AWSAuthConnection
    
    class S3Connection(AWSAuthConnection):
        DefaultHost = boto.config.get('s3', 'host', 's3.amazonaws.com')
        DefaultCallingFormat = boto.config.get('s3', 'calling_format', 'boto.s3.connection.SubdomainCallingFormat')
        QueryString = 'Signature=%s&Expires=%d&AWSAccessKeyId=%s'
    
        def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                     is_secure=True, port=None, proxy=None, proxy_port=None,
                     proxy_user=None, proxy_pass=None,
                     host=DefaultHost, debug=0, https_connection_factory=None,
                     calling_format=DefaultCallingFormat, path='/',
                     provider='aws', bucket_class=Bucket, security_token=None,
                     suppress_consec_slashes=True, anon=False,
                     validate_certs=None):
            if isinstance(calling_format, str):
                calling_format=boto.utils.find_class(calling_format)()
            self.calling_format = calling_format
            self.bucket_class = bucket_class
            self.anon = anon
            AWSAuthConnection.__init__(self, host,
                    aws_access_key_id, aws_secret_access_key,
                    is_secure, port, proxy, proxy_port, proxy_user, proxy_pass,
                    debug=debug, https_connection_factory=https_connection_factory,
                    path=path, provider=provider, security_token=security_token,
                    suppress_consec_slashes=suppress_consec_slashes,
                    validate_certs=validate_certs)

    public by lbottaro @ Amazon_AWS_Python_API modified Aug 4, 2013  1285  2  8  2

    Amazon S3 Aws - Creating a bucket in python

    This code shows how to create a new bucket using boto API on a S3 Amazon account. Please note that the name of the bucket must be unique, different from any other bucket existing in Amazon S3 storage. For this reason you can add some random value to make it unique.
    import boto
    import boto.s3.connection
    access_key = '<aws access key>'
    secret_key = '<aws secret key>'
    
    # This code initialize the Amazon S3 connection to your account
    conn = boto.connect_s3(
            aws_access_key_id = access_key,
            aws_secret_access_key = secret_key)
    
    # This creates a new bucket called myBucket
    bucket = conn.create_bucket('myBucket')

    public by lbottaro @ Amazon_AWS_Python_API modified Aug 4, 2013  1433  2  7  1

    Amazon S3 Aws - get_bucket method in boto python API

    This example shows how to get a single key from a bucket and then write that key to a file.
    from boto.s3.connection import S3Connection
    from boto.s3.key import Key
    
    # Get connection using Amazon secret and access keys
    conn = S3Connection('<aws secret key>', '<aws access key>')
    
    # Get the bucket with name 'bucketname'
    bucket = conn.get_bucket('bucketname')
    
    # Get the key reference to file 'picture.jpg'
    key = bucket.get_key("picture.jpg")
    
    # Open file in write mode
    fp = open ("picture.jpg", "w")
    
    # Set the reference to file
    key.get_file (fp)

    public by lbottaro modified Oct 24, 2013  1286  2  7  1

    Using Pickle To load serialized Objects from file In Python - String Deserialization

    Once you have pickled some data and saved to a txt file, you may want to retrieve the data back to a valid and usable python object at run time. To restore an object to memory, just load the file using pickle, assigning the value to the object itself.
    import pickle 
    
    # The file with serialized data to retrieve
    fileHandler = open(filename, 'r') 
    # The data is stored into my_object variable
    my_object = pickle.load(fileHandler)
    fileHandler.close()
    • Public Snippets
    • Channels Snippets