How to extend datetime.timedelta? - python

I am trying to extend the Python datetime.timedelta for use with cross country race results. I want to construct an object from a string in format u"mm:ss.s". I am able to accomplish this using the factory design pattern and #classmethod annotation. How would I accomplish the same by overriding __init__ and/or __new__?
With the code below, constructing an object raises a TypeError. Note that __init__ is not called, because 'in my __init__' is not printed.
import datetime
import re
class RaceTimedelta(datetime.timedelta):
def __init__(self, timestr = ''):
print 'in my __init__'
m = re.match(r'(\d+):(\d+\.\d+)', timestr)
if m:
mins = int(m.group(1))
secs = float(m.group(2))
super(RaceTimedelta, self).__init__(minutes = mins, seconds = secs)
else:
raise ValueError('timestr not in format u"mm:ss.d"')
Here is the error:
>>> from mytimedelta import RaceTimedelta
>>> RaceTimedelta(u'24:45.7')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported type for timedelta days component: unicode
>>>
If I move my code from __init__ to __new__, I get the following. Note that this time, the output shows that my __new__ function is called.
>>> RaceTimedelta(u'24:45.7')
in my __new__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mytimedelta.py", line 16, in __new__
super(RaceTimedelta, self).__new__(minutes = mins, seconds = secs)
TypeError: datetime.timedelta.__new__(): not enough arguments
>>>

Apparently timedelta objects are immutable, which means their value is actually set in the class' __new__() method—so you'll need to override that method instead of its __init__():
import datetime
import re
class RaceTimedelta(datetime.timedelta):
def __new__(cls, timestr=''):
m = re.match(r'(\d+):(\d+\.\d+)', timestr)
if m:
mins, secs = int(m.group(1)), float(m.group(2))
return super(RaceTimedelta, cls).__new__(cls, minutes=mins, seconds=secs)
else:
raise ValueError('timestr argument not in format "mm:ss.d"')
print RaceTimedelta(u'24:45.7')
Output:
0:24:45.700000
BTW, I find it odd that you're providing a default value for thetimestrkeyword argument that will be considered illegal and raise aValueError.

Related

Python 3: Check if a string is an import command

I want to check a string - is it an import command? I have tried
# Helper - analyses a string - is it an import string?
"""
fromlike - from foo import bar
classic - import foo
classic_as - import foo as baz
"""
def check_is_import(string):
importname = ''
fromlike = False
classic = False
classic_as = False
if string[0:4] is 'from':
fromlike = True
importname = ''
if not fromlike and (string[0:6] is 'import'):
classic = True
importname = string.split(' ')[1]
if classic:
commandlist = string.split(' ')
if commandlist[2] is 'as':
classic_as = True
importname = commandlist[3]
del commandlist
if fromlike:
return ('fromlike', importname)
elif classic and (not classic_as):
return ('classic', importname)
elif classic_as:
return ('classic_as', importname)
else:
return ('no_import', importname)
but it worked for "fromlike" imports. (Note: I'm not asking "why does this code don't work?", I'm just searching a solution) What code will sure detect all imports? Basically my code takes a slice of the string. If the [0:4] slice equals 'from', the string is a "fromlike import". Else: if the [0:6] slice equals 'import', the string is a "classic import". If it detects 'as', it will find the pseudo-name. This function must return a tuple which contains the import type under index 0 and imported module-name under index 1.
If you want to be sure to handle all Python import forms, have Python do the parsing. Use the ast.parse() function and use the resulting parse tree; you'll either get Import or ImportFrom objects:
| Import(alias* names)
| ImportFrom(identifier? module, alias* names, int? level)
Each alias consists of a name and optional identifier used to import the name as:
-- import name with optional 'as' alias.
alias = (identifier name, identifier? asname)
Note that there can be multiple imports! You either have classic or fromlike imports, and both can import multiple names. Your function needs to return a list of (type, name) tuples. For invalid inputs, raise an exception (ValueError is a good fit here):
import ast
def check_is_import(string):
try:
body = ast.parse(string).body
except SyntaxError:
# not valid Python
raise ValueError('No import found')
if len(body) > 1:
# not a single statement
raise ValueError('Multiple statements found')
if not isinstance(body[0], (ast.Import, ast.ImportFrom)):
raise ValueError('No import found')
type_ = 'classic' if isinstance(body[0], ast.Import) else 'fromlike'
results = []
for alias in body[0].names:
alias_type = type_
if alias.asname:
alias_type += '_as'
results.append((alias_type, alias.asname or alias.name))
return results
The method should probably be renamed to extract_import_names(), as that reflects what it does much better.
Demo:
>>> check_is_import('from foo import bar')
[('fromlike', 'bar')]
>>> check_is_import('import foo')
[('classic', 'foo')]
>>> check_is_import('import foo as baz')
[('classic_as', 'baz')]
>>> check_is_import('from foo import bar, baz as spam, monty as python')
[('fromlike', 'bar'), ('fromlike_as', 'spam'), ('fromlike_as', 'python')]
>>> check_is_import('import foo as baz, baz, spam as ham')
[('classic_as', 'baz'), ('classic', 'baz'), ('classic_as', 'ham')]
>>> check_is_import('invalid python')
Traceback (most recent call last):
File "<stdin>", line 3, in check_is_import
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
invalid python
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in check_is_import
ValueError: No import found
>>> check_is_import('import foo; import bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in check_is_import
ValueError: Multiple statements found
>>> check_is_import('1 + 1 == 2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in check_is_import
ValueError: No import found

Patching a method without changing how the method works?

I'm trying to test that a pandas method gets called with some values.
However, just by applying a #patch decorator causes the patched method to throw a ValueError within pandas, when the actual method does not. I'm just trying to test that Stock.calc_sma is calling the underlying pandas.rolling_mean function.
I'm under the assumption that the #patch decorator basically adds some "magic" methods to the thing I'm patching that allow me to check if the function was called. If this is the case, why doesn't the pandas.rolling_mean function behave the same whether it's patched vs. not patched?
app/models.py
import pandas as pd
class Stock: # i've excluded a bunch of class methods, including the one that sets self.data, which is a DataFrame of stock prices.
def calc_sma(self, num_days)
if self.data.shape[0] > num_days: # Stock.data holds a DataFrame of stock prices
column_title = 'sma' + str(num_days)
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
app/tests/TestStockModel.py
def setUp(self):
self.stock = MagicMock(Stock)
self.stock.ticker = "AAPL"
self.stock.data = DataFrame(aapl_test_data.data)
#patch('app.models.pd.rolling_mean')
def test_calc_sma(self, patched_rolling_mean):
Stock.calc_sma(self.stock, 3)
assert(isinstance(self.stock.data['sma3'], Series))
patched_rolling_mean.assert_any_call()
ERROR: test_calc_sma (TestStockModel.TestStockModel)
Traceback (most recent call last):
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
File "/Users/grant/Code/python/chartflux/app/tests/TestStockModel.py", line 26, in test_calc_sma
Stock.calc_sma(self.stock, 3)
File "/Users/grant/Code/python/chartflux/app/models.py", line 27, in calc_sma
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 1887, in __setitem__
self._set_item(key, value)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 1967, in _set_item
value = self._sanitize_column(key, value)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 2017, in _sanitize_column
raise ValueError('Length of values does not match length of '
ValueError: Length of values does not match length of index
>>> import os
>>> os.getcwd()
'/'
>>> from unittest.mock import patch
>>> with patch('os.getcwd'):
... print(os.getcwd)
... print(os.getcwd())
... print(len(os.getcwd()))
...
<MagicMock name='getcwd' id='4472112296'>
<MagicMock name='getcwd()' id='4472136928'>
0
By default patch replaces things with really generic mock objects. As you can see, calling the mock just returns another mock. It has a len of 0 even if the replaced object wouldn't have a len. Its attributes are also generic mocks.
So to simulate behavior requires things extra arguments like:
>>> with patch('os.getcwd', return_value='/a/wonderful/place'):
... os.getcwd()
...
'/a/wonderful/place'
Or to "pass through":
>>> _cwd = os.getcwd
>>> with patch('os.getcwd') as p:
... p.side_effect = lambda: _cwd()
... print(os.getcwd())
...
/
There is a similar example in https://docs.python.org/3.5/library/unittest.mock-examples.html

How can I make mock.mock_open raise an IOError?

I need to test a instance method that calls open. In the first test case, I set mock.mock_open to return a string, as expected. This works wonderfully.
However, I also need to test the case in which an IOError is thrown from this function. How can I make mock.mock_open raise an arbitrary exception?
This is my approach so far:
#mock.patch.object(somemodule, 'generateDefaultKey')
def test_load_privatekey(self, genkey)
mo = mock.mock_open(read_data=self.key)
mo.side_effect = IOError
with mock.patch('__main__.open', mo, create=True):
self.controller.loadPrivkey()
self.assertTrue(genkey.called, 'Key failed to regenerate')
Assign the exception to mock.mock_open.side_effect:
mock.mock_open.side_effect = IOError
From the mock.Mock.side_effect documentation:
This can either be a function to be called when the mock is called, or an exception (class or instance) to be raised.
Demo:
>>> mock = MagicMock()
>>> mock.mock_open.side_effect = IOError()
>>> mock.mock_open()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/mock.py", line 955, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/mock.py", line 1010, in _mock_call
raise effect
IOError
When using patch() as a context manager, a new mock object is produced; assign to that mock object:
with mock.patch('__main__.open', mo, create=True) as mocked_open:
mocked_open.side_effect = IOError()
self.controller.loadPrivkey()
I found Martijn's answer a little hard to generalize, so here is what I hope is a simpler answer less tied to the OPs initial code:
from unittest.mock import patch, mock_open
def func_to_test():
try:
with open('path/to/open', 'r') as file:
return 1
except IOError:
return 2
def test_open_error():
with patch("builtins.open", mock_open()) as mock_file:
mock_file.side_effect = IOError()
assert func_to_test() == 2
mock_file.assert_called_with('path/to/open', 'r')

Python 3 raise TypeError, 'Time.milliseconds expects a datetime object

I just upgraded my mac OS 10.9 environment to Python 3.3 and now when I run a Python lib script I get the following error:
raise TypeError, 'Time.milliseconds expects a date time object
Here's the isolated code that is apparently causing the error:
#classmethod
def to_unix(cls, timestamp):
""" Wrapper over time module to produce Unix epoch time as a float """
if not isinstance(timestamp, datetime.datetime):
raise TypeError, 'Time.milliseconds expects a datetime object'
base = time.mktime(timestamp.timetuple())
return base
And here is the code in the entire section in which the above code resides:
#classmethod
def from_unix(cls, seconds, milliseconds = 0):
""" Produce a full |datetime.datetime| object from a Unix timestamp """
base = list(time.gmtime(seconds))[0:6]
base.append(milliseconds * 1000) # microseconds
return datetime.datetime(* base)
#classmethod
def to_unix(cls, timestamp):
""" Wrapper over time module to produce Unix epoch time as a float """
if not isinstance(timestamp, datetime.datetime):
raise TypeError, 'Time.milliseconds expects a datetime object'
base = time.mktime(timestamp.timetuple())
return base
#classmethod
def milliseconds_offset(cls, timestamp, now = None):
""" Offset time (in milliseconds) from a |datetime.datetime| object to now """
if isinstance(timestamp, (int, float)):
base = timestamp
else:
base = cls.to_unix(timestamp)
base = base + (timestamp.microsecond / 1000000)
if now is None:
now = time.time()
return (now - base) * 1000
which is probably referencing one of these libs:
import date time
import time
Any ideas? I didn't have this error prior to the upgrade so maybe there has been a redefinition of timestamp or datetime?
Thank you
I don't think it has anything to do with that.
Instead, I think you are getting a SyntaxError because your syntax for raise is illegal in Python 3.x:
>>> raise TypeError, 'error'
File "<stdin>", line 1
raise TypeError, 'error'
^
SyntaxError: invalid syntax
Instead, it needs to be like this:
>>> raise TypeError('error')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: error
>>>
Here is a reference.

Python unittesting initiate values

Sorry if this question is stupid. I created an unittest class which needs to take given inputs and outputs from outside. Thus, I guess these values should be initiated. However, I met some errors in the following code:
CODE:
import unittest
from StringIO import StringIO
##########Inputs and outputs from outside#######
a=[1,2]
b=[2,3]
out=[3,4]
####################################
def func1(a,b):
return a+b
class MyTestCase(unittest.TestCase):
def __init__(self,a,b,out):
self.a=a
self.b=b
self.out=out
def testMsed(self):
for i in range(self.tot_iter):
print i
fun = func1(self.a[i],self.b[i])
value = self.out[i]
testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("func1",i,value,fun)
self.assertEqual(round(fun,3),round(value,3),testFailureMessage)
if __name__ == '__main__':
f = MyTestCase(a,b,out)
from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream, verbosity=2)
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
print 'Tests run', result.testsRun
However, I got the following error
Traceback (most recent call last):
File "C:testing.py", line 33, in <module>
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
File "C:\Python27\lib\unittest\loader.py", line 310, in makeSuite
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
File "C:\Python27\lib\unittest\loader.py", line 50, in loadTestsFromTestCase
if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class
Can anyone give me some suggestions? Thanks!
The root of the problem is this line,
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
unittest.makeSuite expects a class, not an instance of a class. So just MyTestCase, not MyTestCase(a, b, out). This means that you can't pass parameters to your test case in the manner you are attempting to. You should probably move the code from init to a setUp function. Either access a, b, and out as globals inside setUp or take a look at this link for information regarding passing parameters to a unit test.
By the way, here is the source file within python where the problem originated. Might be informative to read.

Categories

Resources