Quickfix Float Fields - quickfix

I would like to know the right way to read float fields using Quickfix (python). I was getting a string then casting to float.
For instance:
>>> m = fix.Message()
>>> m.setField(fix.BidPx(1.12))
>>> m.getField(fix.BidPx()).getString()
'1.12'
>>> float(m.getField(fix.BidPx()).getString())
1.12
The way above works fine for floats with less then 15 digits of precision. But I got the following error for float numbers with more the 15 digits of precision:
>>> m = fix.Message()
>>> m.setField(fix.BidPx(1.123456789123456))
>>> m.getField(fix.BidPx()).getString()
'\x00\xe1}\xf5\x82U\x00\x0078912346'
>>> float(m.getField(fix.BidPx()).getString())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:

I am not sure if that sample works, may be you should explain how you import "fix".
Any way, this sample works with python 3.7 and quickfix 1.15.1
>>> import quickfix as fix
>>> m = fix.Message()
>>> m.setField(fix.BidPx(1.123456789123456789123456789))
>>> m.getField(fix.BidPx().getField())
'1.12345678912346'
>>>
if you need more precision in the float number, you can do
>>> m.setField(fix.StringField(fix.BidPx().getField(),"1.123456789123456789123456789"))
>>> m.getField(fix.BidPx().getField())
'1.123456789123456789123456789'
>>>
I hope I've helped

Related

pycairo error: “no current point defined” when trying to use TeeSurface

I'm trying to use a cairo.TeeSurface (pycairo version '1.21.0') to redirect input to multiple surfaces, but I cannot make it work, here is my code:
>>> import cairo
>>> s1 = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500,500)
>>> s2 = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500,500)
>>> s3 = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500,500)
>>> s = cairo.TeeSurface(s1)
>>> s.add(s2)
>>> s.add(s3)
>>> c = cairo.Context(s)
After above iniziatilation if I try to draw a line I get an error:
>>> c.rel_line_to(100,100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cairo.Error: no current point defined
I tried to set explicitly a point but I get the same error:
>>> c.move_to(10,10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cairo.Error: no current point defined
For sure I must have misunderstood something, any clue would be appreciated.

Micropython: Non-Blocking SSLSocket

I was just trying SSL-Sockets in Micropython and discovered that I can not set the connection to non-blocking as the setblocking() function is not implemented, yet.
>>> import ussl
>>> import usocket
>>> s = usocket.socket()
>>> adr = usocket.getaddrinfo('myserverwithssl.com', 443)[0][-1]
>>> s.connect(adr)
>>> s_ssl = ussl.wrap_socket(adr)
>>> s_ssl.setblocking(False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError:
Looking forward to the implementation. However, is there another way to achieve nonblocking SSL-Sockets in Micropython?

Url encode using python 2.7

>>> import httplib
>>> x = httplib.HTTPConnection('localhost', 8080)
>>> x.connect()
>>> x.request('GET','/camera/store?fn=aaa&ts='+str.encode('2015-06-15T14:45:21.982600+00:00','ascii')+'&cam=ddd')
>>> y=x.getresponse()
>>> z=y.read()
>>> z
'error: Invalid format: "2015-06-15T14:45:21.982600 00:00" is malformed at " 00:00"'
And the system show me this error. As i want to encode this format to this: 2015-06-15T14%3A45%3A21.982600%2B00%3A00
>>> import urllib
>>> f = { 'fn' : 'aaa', 'ts' : "2015-06-15T14:45:21.982600+00:00"}
>>> urllib.urlencode(f)
from:
How to urlencode a querystring in Python?
url = "http://example.com?p=" + urllib.quote(query)
it works with this!

Jython 2.5 isdigit

I am trying to add an isdigit() to the program so that I can verify what the user enters is valid. This is what I have so far. But when I run it an enter a character, say "f". It crashes and gives me the error which will be posted below the code. Any ideas?
def mirrorHorizontal(source):
userMirrorPoint = requestString("Enter a mirror point from 0 to halfway through the pitcure.") #asks user for an input
while (int(userMirrorPoint) < 0 or int(userMirrorPoint) > (int(getHeight(source) - 1)//2)) or not(userMirrorPoint.isdigit()):
userMirrorPoint = requestString("Enter a mirror point from 0 to halfway through the pitcure.")
height = getHeight(source)
mirrorPoint = int(userMirrorPoint)
for x in range(0, getWidth(source)):
for y in range(0, mirrorPoint):
topPixel = getPixel(source, x, y)
bottomPixel = getPixel(source, x, height-y-1)
color = getColor(topPixel)
setColor(bottomPixel, color)
The error was: f
Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
Please check line 182 of /Volumes/FLASHDRIVE2/College/Spring 16'/Programs - CPS 201/PA5Sikorski.py
isdigit() itself behaves itself in the 2.7.0 jython version I have locally
>>> '1'.isdigit()
True
>>> ''.isdigit()
False
>>> 'A'.isdigit()
False
>>> 'A2'.isdigit()
False
>>> '2'.isdigit()
True
>>> '22321'.isdigit()
True
Try breaking your big expression up, as typecasting to integers will throw errors for non-numeric strings. This is true across Python versions.
>>> int('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'b'
>>> int('2')
2
You probably want to be careful about the order of the parts of that long expression (this or that or ...). Breaking it up would also make it more readable.

Print proper mathematical formatting

When I use sympy to get the square root of 8, the output is ugly:
2*2**(1/2)
import sympy
In [2]: sympy.sqrt(8)
Out[2]: 2*2**(1/2)
Is there any way to make sympy print output in proper mathematical notation (i.e. using the proper symbol for square root) ?
UPDATE:
when I follow the suggestions from #pqnet:
from sympy import *
x, y, z = symbols('x y z')
init_printing()
init_session()
I get following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-21d886bf3e54> in <module>()
2 x, y, z = symbols('x y z')
3 init_printing()
----> 4 init_session()
/usr/lib/python2.7/dist-packages/sympy/interactive/session.pyc in init_session(ipython, pretty_print, order, use_unicode, quiet, argv)
154 # and False means don't add the line to IPython's history.
155 ip.runsource = lambda src, symbol='exec': ip.run_cell(src, False)
--> 156 mainloop = ip.mainloop
157 else:
158 mainloop = ip.interact
AttributeError: 'ZMQInteractiveShell' object has no attribute 'mainloop'
In an ipython notebook you can enable Sympy's graphical math typesetting with the init_printing function:
import sympy
sympy.init_printing(use_latex='mathjax')
After that, sympy will intercept the output of each cell and format it using math fonts and symbols. Try:
sympy.sqrt(8)
See also:
Printing section in the Sympy Tutorial.
The simplest way to do it is this:
sympy.pprint(sympy.sqrt(8))
For me (using rxvt-unicode and ipython) it gives
___
2⋅╲╱ 2