Converting the input of DecimalField of WTForm to a float?-Flask - python-2.7

The input I give to a DecimalField of WTFform in Flask application is being converted to a string. I need it be an int or float in order to perform any
mathematical operation?
Any help would be appreciated.
TIA

wtforms.DecimalField produces a decimal.Decimal instance.
>>> import wtforms
>>> from webob.multidict import MultiDict
>>> class F(wtforms.Form):
... foo = wtforms.DecimalField()
...
>>> f = F(formdata=MultiDict(foo=3.45))
>>> val = f.data['foo']
>>> val
Decimal('3.45000000000000017763568394002504646778106689453125')
>>> type(val)
<class 'decimal.Decimal'>
The object can be used in calculations:
>>> val * 4
Decimal('13.80000000000000071054273576')
>>> val + 2
Decimal('5.450000000000000177635683940')
or can be converted to a float or int using the respective builtin functions
>>> float(val)
3.45
>>> int(val)
3

Related

Periodicity of SymPy trigonometric function

Sympy's trigonometric functions takes periodic argument into account.
from sympy import pi, sin, Symbol
n = Symbol('n', integer=True)
>>> sin(2*pi + 4)
sin(4)
>>> sin(n*pi)
0
However, it seems that it does not support this feature...
n = Symbol('n', integer=True)
>>> sin(2*n*pi + 4)
sin(2*n*pi + 4) # Expected sin(4)
.simplify() or .doit() was not working. Is there any function or method to convert sin(2*n*pi + 4) to sin(4)?
You could use trigsimp or seemingly clunky expansion and rewriting:
>>> eq = sin(2*n*pi + 4)
>>> eq.rewrite(exp).expand().rewrite(sin).expand()
sin(4)
>>> trigsimp(eq)
sin(4)

Django Sum Aggregation from Int to Float

So i save my fees in cents, but want to display them in Euro. The conversion problem is that it first divides the integer by 100 and then converts to a float. It should be the other way around. How can i do that?
The following code should illustrate my Problem:
>>> from django.db.models import F, FloatField, Sum
>>> from payment.models import Payment
>>>
>>> paymentlist = Payment.objects.all()
>>> result = paymentlist.annotate(
... fees_in_cents=Sum(F('fees'), output_field=FloatField())).annotate(
... fees_in_euro=Sum(F('fees')/100, output_field=FloatField()))
>>>
>>> print(result[0].fees_in_cents, result[0].fees_in_euro)
120.0 1.0
fees_in_euro should obviously be 1.20
Divide fees value by 100.0
Example:
result = paymentlist.annotate(
fees_in_cents=Sum(F('fees'), output_field=FloatField())).annotate(
fees_in_euro=Sum(F('fees') / 100.0, output_field=FloatField())
)

creating a byte string in python

I'm trying to create a byte string, but it seems to be just a regular character string. What am I doing wrong here?
byteStr = b'some string'
byteStr #'some string'
utfStr = 'some string'.encode('utf-8')
utfStr #'some string'
byteStr == utfStr #True
If you're trying to create a byte array in Python 2, it's called a bytearray. Python 2 does not have a byte string.. The b in front of the str is ignored in Python 2, meaning 'hello' == b'hello'
Try this:
>>> f = b'f'
>>> type(f)
<type 'str'>
Now, it's important to remember that u'f' == 'f':
>>> h = u'f'
>>> f == h
True
>>> type(h)
>>> <type 'unicode'>

Django: Identify the urls that provide duplicate content and set a canonical link

models.py
class People(models.Model):
name = models.CharField(max_length=16)
meters_away = models.IntegerField()
Lets populate the db:
>>> from people.models import People
>>> a = People()
>>> a.name = 'George'
>>> a.meters_away = 15
>>> a.save()
>>> b = People()
>>> b.name = 'Jim'
>>> b.meters_away = 10
>>> b.save()
Supposing that we have a url that returns all people in a range of x meters:
http://example.com/range/<meters>
This url scheme accepts 3 hits as follows:
http://example.com/range/20
http://example.com/range/30
http://example.com/range/40
Those hits will create the following queries:
>>> hit1 = People.objects.filter(meters_away__lt=20)
>>> hit2 = People.objects.filter(meters_away__lt=30)
>>> hit3 = People.objects.filter(meters_away__lt=40)
Where:
>>> list(hit1) == list(hit2) == list(hit3)
>>> True
This means that example.com, will serve 3 different urls with the same content.
From a SEO point of view, how could all the possible urls (meters: 21, 22, 23, 24, 30, 40 etc) be filtered in a way that a canonical url is appended to them?
The way i understood your question, you may want to get the maximum distance in meters that produce the same result as the current distance (say m meters):
next_number = People.objects.filter(meters_away__gte=m).order_by('meters_away')[:1]
next_number = next_number[0] if next_number else m
and the canonical url will be:
http://example.com/range/<next_number>

Converting a Set to Dict in python

I have a set like this.
x = set([u'[{"Mychannel":"sample text"},"p"]'])
I need to convert it into Dict.
I need to get output as
x = {'mychannel':'sampletext'}
How to do this.
It looks like you can unpack that crazy thing like this:
>>> x = set([u'[{"Mychannel":"sample text"}, "p"]'])
>>> lst = list(x)
>>> lst
[u'[{"Mychannel":"sample text"}, "p"]']
>>> lst[0]
u'[{"Mychannel":"sample text"}, "p"]'
>>> inner_lst = eval(lst[0])
>>> inner_lst
[{'Mychannel': 'sample text'}, 'p']
>>> d = inner_lst[0]
>>> d
{'Mychannel': 'sample text'}
However, as #MattDMo suggests in comments, I seriously suggest you re-evaluate this data structure, if not at least to factor out the step where you need eval to use it!