repr

Django: Assigning ForeignKey - Unable to get repr for class

佐手、 提交于 2019-12-10 16:53:54
问题 I ask this question here because, in my searches, this error has been generally related to queries rather than ForeignKey assignment. The error I am getting occurs in a method of a model. Here is the code: class Deal(models.Model): ...model_fields... def _update_existing_deal(self, deal_dict): #deal made from deal_dict here, psuedo code below deal = Deal(deal_dict) HistoricalDeal().create_historical_deal(deal) self.price = deal_dict.get('price', self.price) if self.comment != deal_dict.get[

Recursive reference to a list within itself [duplicate]

丶灬走出姿态 提交于 2019-12-10 01:52:52
问题 This question already has answers here : Why does list(my_list) modify the object? (2 answers) Closed 5 years ago . So I came across something very weird in python. I tried adding a reference to the list to itself. The code might help demonstrate what I am saying better than I can express. I am using IDLE editor(interactive mode). >>>l=[1,2,3] >>>l.append(l) >>>print(l) [1,2,3,[...]] >>>del l[:-1] >>>print(l) [[...]] So far the output is as expected. But when I do this. y=l[:] print(y) To me

Which is a better __repr__ for a custom Python class?

只愿长相守 提交于 2019-12-08 17:15:20
问题 It seems there are different ways the __repr__ function can return. I have a class InfoObj that stores a number of things, some of which I don't particularly want users of the class to set by themselves. I recognize nothing is protected in python and they could just dive in and set it anyway, but seems defining it in __init__ makes it more likely someone might see it and assume it's fine to just pass it in. (Example: Booleans that get set by a validation function when it determines that the

`repr` and `int` take quadratic time in Python

非 Y 不嫁゛ 提交于 2019-12-08 05:48:18
问题 I was making a table of different run-times for Python 2.7, and noticed a thing that I cannot explain: The run-time of repr(2**n) and int('1'*n) is O(n^2) . I always assumed that converting between integer and string would be O(n) with n being number of digits. The results show that if O(n) fitting gives ~30% error, while O(n^2) is only ~5%. Could anyone explain please. Here are the results that I get (codes are below): Test Number-1 -- time to compute int('1'*n) (fit to O(n**2)) Spec_string:

`repr` and `int` take quadratic time in Python

99封情书 提交于 2019-12-07 09:55:30
I was making a table of different run-times for Python 2.7, and noticed a thing that I cannot explain: The run-time of repr(2**n) and int('1'*n) is O(n^2) . I always assumed that converting between integer and string would be O(n) with n being number of digits. The results show that if O(n) fitting gives ~30% error, while O(n^2) is only ~5%. Could anyone explain please. Here are the results that I get (codes are below): Test Number-1 -- time to compute int('1'*n) (fit to O(n**2)) Spec_string: 1000<=n<=10000 by factors of 2 var_list ['n'] Function list: ('n**2', 'n', '1') run times: n = 1000 :

Apply control characters to a string - Python

对着背影说爱祢 提交于 2019-12-07 07:51:27
问题 I'm trying to apply control characters, such as '\x08 \x08' that should remove the precedent char, to a string (move backwards, write space, move backwards) For example when I type into python console : s = "test\x08 \x08" print s print repr(s) I get in my terminal : tes 'test\x08 \x08' I'm looking for a function, let's says "function", that will 'apply' control characters to my string : v = function("test\x08 \x08") sys.stdout.write(v) sys.stdout.write(repr(v)) so I get a "clean", control

Formatting floating-point numbers without loss of precision in AngularJS

大城市里の小女人 提交于 2019-12-05 22:05:57
In AngularJS how do I output a floating point number on an HTML page without loss of precision and without unnecessary padding with 0's? I've considered the "number" ng-filter ( https://docs.angularjs.org/api/ng/filter/number ) but the fractionSize parameter causes a fixed number of decimals: {{ number_expression | number : fractionSize}} I'm looking for what in various other languages is referred to as "exact reproducibility", "canonical string representation", repr, round-trip, etc. but I haven't been able to find anything similar for AngularJS. For example: 1 => "1" 1.2 => "1.2" 1.23456789

Python: Maximum recursion depth exceeded when printing custom exception

a 夏天 提交于 2019-12-05 02:45:25
The following code throws RuntimeError: maximum recursion depth exceeded while getting the str of an object . I can resolve the infinite recursion in two different ways, but I don't understand why each fix works and thus don't know which to use, or if either are correct. class FileError( Exception ): def __init__( self, filename=None, *a, **k ): #Fix 1: remove super super( FileError, self ).__init__( self, *a, **k ) self.filename = filename def __repr__( self ): return "<{0} ({1})>".format( self.__class__.__name__, self.filename ) #Fix 2: explicitly define __str__ #__str__ = __repr__ print(

Recursive reference to a list within itself [duplicate]

痞子三分冷 提交于 2019-12-05 01:06:12
This question already has answers here : Why does list(my_list) modify the object? (2 answers) Closed 5 years ago . So I came across something very weird in python. I tried adding a reference to the list to itself. The code might help demonstrate what I am saying better than I can express. I am using IDLE editor(interactive mode). >>>l=[1,2,3] >>>l.append(l) >>>print(l) [1,2,3,[...]] >>>del l[:-1] >>>print(l) [[...]] So far the output is as expected. But when I do this. y=l[:] print(y) To me it seems that the output should be [[...]] But it is [[[...]]] Apparently instead of creating a copy of

Correct way to write __repr__ function with inheritance

和自甴很熟 提交于 2019-12-04 16:45:33
问题 I'm experimenting with OOP python and I wasn't sure about the __repr__ function inheritance. Since the parent class function looked like this: def __repr__(self): '''Returns representation of the object''' return("{}({!r})".format("Class name", self._param)) I wanted to know if it is better to use a generic approach (which could be suitable for children classes as well) like the following one: def __repr__(self): '''Returns representation of the object''' return("{}({!r})".format(self.__class