Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, March 29, 2017

Working with zipfile module Python

Memo related to zipfile module and most common methods from this module.

import zipfile

compressedFile = zipfile.ZipFile('test.zip')

compressedFile.namelist() - returns list of files and directories in archive

fileInfo = compressedFile.getinfo('file') - returns information about file in archive

fileInfo.file_size - returns size of the file

fileInfo.compress_size - returns compression ratio

compressedFile.extractall() - extracts everything from zip into current directory

compressedFile.extractall(path) - extracts everything from zip to destination directory

compressedFile.extract('file', path) - extracts exact file from zip to destination path


newZip = zipfile.ZipFile('test.zip', 'w')
newZip.write('test.txt', compress_type=zipfile.ZIP_DEFLATED)
newZip.close() - adding file to archive however this will overrides all archive

zipfile.ZipFile('test.zip', 'a') - adding file to archive without overriding 


Working with files in Python (part 2) - shutil, move2trash, os

In this memo I'll save some common methods related to shutil, move2trash modules and some more from os module.

shutil.

import shutil

shutil.copy('source', 'destination') - copies file or directory

shutil.copytree('source', 'destination') - copies directory with all subdirectories & files

shutil.move('source', 'destination') - moves file or directory

shutil.remtree(path) - remove directory with all subdirectories


send2trash.

import send2trash

send2trash.send2trash(path) - moves file or directory to the bin


os.

import os

os.walk(path) - returns list of all directories, subdirectories and files

os.unlink(path) - permanently deletes file

os.rmdir(path) - permanently removes directory

Monday, March 27, 2017

Working with files in Python

Some notes about working with files in Python.

How simply generate path:

import os
os.path.join('usr', 'user', 'Documents')

or if we need path to file:

os.path.join('home/Documents/work', important.txt)

os.getcwd()check current directory

os.chdir('path to new working directory')change directory

os.makedirs('home/docs/test')create directory

os.path.abspath('path')get absolute path

os.path.relpath('path', 'start point')get relative path

os.path.dirname('path')get directory name actually will return name with whole path

os.path.basename('path')get file name

os.path.split('path')to get both directory name and file name as a tuple

or 

examplePath.split(os.path.sep) - will return tuple where every directory and file name are separate strings (in OSX first item in the list will be '')

os.path.listdir('path')list all items in the directory

os.path.getsize('path')get file size in bytes

os.path.exists('path') - returns True if directory or file exists

os.path.isfile('path') - returns True if file exists

os.path.isdir('path') - returns True if directory exists

open('path') - returns file object if there's no such file Python will create that

read('path') or open('path', 'r')- reads file

file.readlines() - reads line by line from file

write('path') or open('path', 'w')- writes to file (simple usage will overwrite all content)

open('path', 'a') - will append text to the end of the file instead of overwriting

close('path') - closes file


Work with binary shelf files (can be used as dictionaries etc.):

import shelve
testFile = shelve.open('filename')
testValues = ['email', 'address', 'phone']
testFile['data'] = testValues
testFile.close()

testFile('data') - prints out values

list(testFile.keys()) - prints list of keys in our case it will be just 'data'

list(testFile.values()) - prints list of values ie ['email', 'address', 'phone']

import pprint
pprint.pformat(list) - will represent list / dictionary as a string for easier saving that to a file

And for the end short reminder about shelf files and plain text files: first used mostly for complex objects (ie file objects) while plain text files can be used for storing simple data (ie text, integers etc)


Sunday, March 26, 2017

Regex rules for Python...

Something to keep in mind about Regex.

? - matches zero or one of the preceding group
*matches zero or more of the preceding group
+matches one or more of the preceding group
{n} - matches exactly n of the preceding group
{n,} - matches n or more of the preceding group 
{,m} - matches 0 to m of the preceding group 
{n,m} - matches at least n and at most m of the preceding group
{n,m}? or *? or +? - performs a nongreedy match of the preceding group 
^spam - means the string must begin with spam
spam$ - means the string must end with spam
. - matches any character, except newline characters
\d, \w and \s - match a digit, word, or space character
\D, \W and \S - match anything except a digit, word or space character
[abc] - matches any character between the brackets
[^abc] - matches any character that isn't between brackets


In order to use  package re should be imported after that compile method can be invoked:

regex = re.compile(r'spam')

Some useful methods:

regex.search('abc') - returns first occurrence of matched pattern
regex.group(1) - returns first group in case of multiple of those ie re.compile(r'(\d\d)-(\d\d\d))
regex.group() - returns the entire matched text
regex.groups() - returns tuple of multiple values
regex.findall() - returns the strings of every match in the searched string ie list of strings if there's more than 1 match
regex.sub() - takes two arguments: first string for replacement and second string where to replace (in case of match) ie regex.sub(r'abc', 'abc is the most popular shortcut')

List is not complete and I will try to add some more examples later...


Monday, April 4, 2016

Use Multiple Python Versions In OSX

This post aims to help with managing several different Python versions on OSX without any troubles.

As usually just follow next steps:

1. $ brew update

2. $ brew install pyenv

3. Add into .bashrc: 

" if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi"

4. Shows list of options

$ pyenv install -l  

5. Installs needed python version

$ pyenv install X.XX.XX 

6. Shows installed python versions in system

$ pyenv versions 

7. Sets specific version as global one

$ pyenv global X.XX.XX 

8. Sets specific version as local one

$ pyenv local X.XX.XX 

9. Unsets local version for python

$ pyenv local --unset 

For further exploring:

10. Shows list of available commands for pyenv

$ pyenv commands 

This is pretty much enough for this topic (at least for now).

Tuesday, November 11, 2014

Your first test with Python + WebDriver + PyCharm

Today I’d like You to try Python for Your functional Web tests. Why Python? Because I believe it’s much easier to start with if we’ll compare with Java or .Net and also in several cases Python (because it’s scripting language) can do much more things than even Java (with all cross platform functionality itself).
So let’s do everything from the scratch. Next screens and instructions are for Windows users because I’m sure that install and run Python on OSX or Linux is much easier and doesn’t need any specific explanations.

1. Install Python.

For this we’re going to https://www.python.org/downloads/ and download needed package.




As You can see that we’re able to download Windows installer and install Python without any problems – I did it in default directory, also have to underline that I’ll use Python 3.4.2 for this example. 


After installation we’ll need to create local variable PYTHON_HOME (which is really not mandatory, but will be useful) and check if Python added to the main Path. For that just follow screens below one by one.







If everything correct we have to able to see next screen after running command “python --version” in our command prompt.


And this is pretty much all for Python installation.

2. Selenium installation.

Here will be also pretty much straightforward process with adding Python language bindings for WebDriver. For Python we don’t need to download different browser drivers as we did for Java.
First open https://pypi.python.org/pypi/selenium – and download archive with selenium (see screen below). After downloading extract somewhere on the disk. Next go to Your Python folder e.g. “C:\Python34” and run command (in command line prompt) – “pip install –U selenium”. 




 After this command Python should install Selenium but I faced one issue during this installation and for that we need our extracted Selenium archive – You have to copy extracted folder “selenium” (with all files in it) to “C:\Python34\Lib” (see screen below). 


One more step – we have to ensure that “selenium” is also in the path variable (scree below).


We’re almost ready, actually we’re ready for writing and running scripts in Python. As I mentioned before Python scripting language so it doesn’t need any specific IDE or anything like that – we can use simple notepad for testing. But for future convenience I’d recommend You install PyCharm from JetBrains.

3. Installing PyCharm

I believe one really easy step – go to https://www.jetbrains.com/pycharm/download/ and download community edition, which totally free and provides all necessary functionality. For those who use IntelliJ Idea already – You just need to add Python language plugin and then create new Python project (also pretty simple)


After downloading and installing PyCharm we’re all set, so we have just one more step – actual test writing and running.

4. Writing and running first test.

On first two screens we’re creating new project in PyCharm – pretty straight forward process.



Then we have to check our settings for ensure our PyCharm will use needed Python interpreter. So go : “File > Settings > Project interpreter” or press “Ctrl + Alt + S” and make sure that correct Python distributive selected also there should be “selenium” in the list of packages (see screen below).


Last step – create a “test” package in project root directory and then Python file – e.g. “FirstWebDriverTest.py”.
Our test will do several simple steps:
    - Open browser (for this example I’ll use Chrome, but You can easily change that).
    - Navigate to http://www.allegro.pl.
    - Type in search box “Laptop”.
    - Press “Search” button.
    - Verify that first item from results list has “Laptop” in it’s title.


For Your convenience I’ll paste whole code below:

import unittest  
 from selenium import webdriver  
   
 class FirstWebDriverTest(unittest.TestCase):  
   
   def setUp(self):  
     self.driver = webdriver.Chrome()  
     self.driver.get("http://www.allegro.pl")  
     self.SEARCH_TEXT = "Laptop"  
     self.SEARCH_FIELD_ID = "//input[@id='main-search-text']"  
     self.SEARCH_BUTTON_ID = "//input[@class='search-btn']"  
     self.SEARCH_RESULT_ITEM_TITLE = "//article[@class='offer offer-brand']//h2//span"  
   
   def test_search_in_allegro_pl(self):  
     driver = self.driver  
   
     driver.find_element_by_xpath(self.SEARCH_FIELD_ID)\  
       .send_keys(self.SEARCH_TEXT)  
     search_button = driver.find_element_by_xpath(self.SEARCH_BUTTON_ID)  
     search_button.submit()  
   
     driver.implicitly_wait(5000)  
     result = driver.find_elements_by_xpath(self.SEARCH_RESULT_ITEM_TITLE)\  
       .__getitem__(0)\  
       .__getattribute__("text")  
     self.assertIn(self.SEARCH_TEXT, result)  
   
   def tearDown(self):  
     self.driver.close()  


If You’ll do copy and paste from this page make sure that after inserting code has original formatting (You can refer to screen above) because for Python it’s extremely important.
So now we can run our test – “Run > Run” or “Alt + Shift + F10
Also You can do it through command prompt line (see screen below)


Hope this will be easy for You to start writing tests in Python and You’ll check how actually powerful this language is. One last thing – for this example with all downloading and installing steps I’ve spent about one hour (actually I’ve spent more for doing screens and describing steps :) ).

P.S. added project to repo: https://github.com/operep/Python_Test_Project.git, so now You'll be able simply clone that