Call Python class methods from the command line - python

so I wrote some class in a Python script like:
#!/usr/bin/python
import sys
import csv
filepath = sys.argv[1]
class test(object):
def __init__(self, filepath):
self.filepath = filepath
def method(self):
list = []
with open(self.filepath, "r") as table:
reader = csv.reader(table, delimiter="\t")
for line in reader:
list.append[line]
If I call this script from the command line, how am I able to call method?
so usually I enter: $ python test.py test_file
Now I just need to know how to access the class function called "method".

You'd create an instance of the class, then call the method:
test_instance = test(filepath)
test_instance.method()
Note that in Python you don't have to create classes just to run code. You could just use a simple function here:
import sys
import csv
def read_csv(filepath):
list = []
with open(self.filepath, "r") as table:
reader = csv.reader(table, delimiter="\t")
for line in reader:
list.append[line]
if __name__ == '__main__':
read_csv(sys.argv[1])
where I moved the function call to a __main__ guard so that you can also use the script as a module and import the read_csv() function for use elsewhere.

Open Python interpreter from the command line.
$ python
Import your python code module, make a class instance and call the method.
>>> import test
>>> instance = test(test_file)
>>> instance.method()

Related

How do I mock `csv.DictWriter.writeheader()`, which is called from within my custom function?

I have a custom function defined in
custom_file.py
import csv
def write_dict_to_csv(columns=None, file_name=None, data=None):
try:
with open(file_name, "w") as f:
writer = csv.DictWriter(f, fieldnames=columns)
writer.writeheader()
in test_file.py I want to return a fixed value when writer.writeheader() is called.
from custom_file import write_dict_to_csv
class TestMyFunctions(unittest.TestCase):
#patch('custom_file.csv.DictWriter.writeheader')
def test_write_dict_to_csv(self, mock_writeheader):
custom_file.write_dict_to_csv(file_name='fileName')
self.assertTrue(mock_writeheader.called)
But this returns TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
How do I mock csv.DictWriter.writeheader() when it's being imported from an external library into a custom_file, which I'm then testing from a separate test_file?
I figured this would be close to correct since we're meant to patch where a thing is looked up, not where it is defined.
The code you have provided does not run "as is" because of some missing imports, but after fixing the problems everything seems working (the test passes).
Here is the code I ran. I hope it helps.
custom_file.py
import csv
def write_dict_to_csv(columns=None, file_name=None, data=None):
with open(file_name, "w") as f:
writer = csv.DictWriter(f, fieldnames=columns)
writer.writeheader()
test_file.py
import unittest
from unittest.mock import patch
import custom_file
class TestMyFunctions(unittest.TestCase):
#patch('custom_file.csv.DictWriter.writeheader')
def test_write_dict_to_csv(self, mock_writeheader):
print("Hello")
custom_file.write_dict_to_csv(file_name='fileName')
self.assertTrue(mock_writeheader.called)

pickle is not working in a proper way

import nltk
import pickle
input_file=open('file.txt', 'r')
input_datafile=open('newskills1.txt', 'r')
string=input_file.read()
fp=(input_datafile.read().splitlines())
def extract_skills(string):
skills=pickle.load(fp)
skill_set=[]
for skill in skills:
skill= ''+skill+''
if skill.lower() in string:
skill_set.append(skill)
return skill_set
if __name__ == '__main__':
skills= extract_skills(string)
print(skills)
I want to print the skills from file but, here pickle is not working.
It shows the error:
_pickle.UnpicklingError: the STRING opcode argument must be quoted
The file containing the pickled data must be written and read as a binary file. See the documentation for examples.
Your extraction function should look like:
def extract_skills(path):
with open(path, 'rb') as inputFile:
skills = pickle.load(inputFile)
Of course, you will need to dump your data into a file open as binary as well:
def save_skills(path, skills):
with open(path, 'wb') as outputFile:
pickle.dump(outputFile, skills)
Additionally, the logic of your main seems a bit flawed.
While the code that follows if __name__ == '__main__' is only executed when the script is run as main module, the code that is not in the main should only be static, ie definitions.
Basically, your script should not do anything, unless run as main.
Here is a cleaner version.
import pickle
def extract_skills(path):
...
def save_skills(path, skills):
...
if __name__ == '__main__':
inputPath = "skills_input.pickle"
outputPath = "skills_output.pickle"
skills = extract_skills(inputPath)
# Modify skills
save_skills(outputPath, skills)

Using pytest to ensure a file is created and written to

I'm using pytest and want to test that a function writes some content to a file. So I have writer.py which includes:
MY_DIR = '/my/path/'
def my_function():
with open('{}myfile.txt'.format(MY_DIR), 'w+') as file:
file.write('Hello')
file.close()
I want to test /my/path/myfile.txt is created and has the correct content:
import writer
class TestFile(object):
def setup_method(self, tmpdir):
self.orig_my_dir = writer.MY_DIR
writer.MY_DIR = tmpdir
def teardown_method(self):
writer.MY_DIR = self.orig_my_dir
def test_my_function(self):
writer.my_function()
# Test the file is created and contains 'Hello'
But I'm stuck with how to do this. Everything I try, such as something like:
import os
assert os.path.isfile('{}myfile.txt'.format(writer.MYDIR))
Generates errors which lead me to suspect I'm not understanding or using tmpdir correctly.
How should I test this? (If the rest of how I'm using pytest is also awful, feel free to tell me that too!)
I've got a test to work by altering the function I'm testing so that it accepts a path to write to. This makes it easier to test. So writer.py is:
MY_DIR = '/my/path/'
def my_function(my_path):
# This currently assumes the path to the file exists.
with open(my_path, 'w+') as file:
file.write('Hello')
my_function(my_path='{}myfile.txt'.format(MY_DIR))
And the test:
import writer
class TestFile(object):
def test_my_function(self, tmpdir):
test_path = tmpdir.join('/a/path/testfile.txt')
writer.my_function(my_path=test_path)
assert test_path.read() == 'Hello'

Import a module with optional arguments python

Currently, I have a file called utils.py where I keep all my functions and another file called main.py.
In my utils file, I have a two functions that load and save to a json file, along with a bunch of other functions that will edit the data.
def save_league(league_name, records):
with open('%s.json' % league_name, 'w') as f:
f.write(json.dumps(records))
def load_league(league_name):
with open('%s.json' % league_name, 'r') as f:
content = f.read()
records = json.loads(content)
return records
I am trying to add optional arguments for the save_league function by changing the function to:
def save_league(name = league_name, r = records):
with open('%s.json' % name, 'w') as f:
f.write(json.dumps(r))
This way the file will save just from save_league().
However, when I try to import a function with optional arguments in main.py, I get a name error because the default arguments are not set at the beginning.
NameError: name 'league_name' is not defined
Is it possible import functions with optional args into another file or do I have to combine the two files into one?

Pythonic equivalent of ./foo.py < bar.png

I've got a Python program that reads from sys.stdin, so I can call it with ./foo.py < bar.png. How do I test this code from within another Python module? That is, how do I set stdin to point to the contents of a file while running the test script? I don't want to do something like ./test.py < test.png. I don't think I can use fileinput, because the input is binary, and I only want to handle a single file. The file is opened using Image.open(sys.stdin) from PIL.
You should generalise your script so that it can be invoked from the test script, in addition to being used as a standalone program. Here's an example script that does this:
#! /usr/bin/python
import sys
def read_input_from(file):
print file.read(),
if __name__ == "__main__":
if len(sys.argv) > 1:
# filename supplied, so read input from that
filename = sys.argv[1]
file = open(filename)
else:
# no filename supplied, so read from stdin
file = sys.stdin
read_input_from(file)
If this is called with a filename, the contents of that file will be displayed. Otherwise, input read from stdin will be displayed. (Being able to pass a filename on the command line might be a useful improvement for your foo.py script.)
In the test script you can now invoke the function in foo.py with a file, for example:
#! /usr/bin/python
import foo
file = open("testfile", "rb")
foo.read_input_from(file)
Your function or class should accept a stream instead of choosing which stream to use.
Your main function will choose sys.stdin.
Your test method will probably choose a StringIO instance or a test file.
The program:
# foo.py
import sys
from PIL import Image
def foo(stream):
im = Image.open(stream)
# ...
def main():
foo(sys.stdin)
if __name__ == "__main__":
main()
The test:
# test.py
import StringIO, unittest
import foo
class FooTest(unittest.TestCase):
def test_foo(self):
input_data = "...."
input_stream = StringIO.StringIO(input_data)
# can use a test file instead:
# input_stream = open("test_file", "rb")
result = foo.foo(input_stream)
# asserts on result
if __name__ == "__main__":
unittest.main()
A comp.lang.python post showed the way: Substitute a StringIO() object for sys.stdout, and then get the output with getvalue():
def setUp(self):
"""Set stdin and stdout."""
self.stdin_backup = sys.stdin
self.stdout_backup = sys.stdout
self.output_stream = StringIO()
sys.stdout = self.output_stream
self.output_file = None
def test_standard_file(self):
sys.stdin = open(EXAMPLE_PATH)
foo.main()
self.assertNotEqual(
self.output_stream.getvalue(),
'')
def tearDown(self):
"""Restore stdin and stdout."""
sys.stdin = self.stdin_backup
sys.stdout = self.stdout_backup
You can always monkey patch Your stdin. But it is quite ugly way. So better is to generalize Your script as Richard suggested.
import sys
import StringIO
mockin = StringIO.StringIO()
mockin.write("foo")
mockin.flush()
mockin.seek(0)
setattr(sys, 'stdin', mockin)
def read_stdin():
f = sys.stdin
result = f.read()
f.close()
return result
print read_stdin()
Also, do not forget to restore stdin when tearing down your test.

Categories

Resources