[Tutor] windows specific: read process memory

2017-09-25 Thread Michael C
Hi all, I have a few questions about memory scanning!
The following code attempts to print out all addresses whose value is
int(-143)!

1. I am using a for loop to go through all the addresses! Is this the right
thing to do?
2. I feed the read process memory function with hex(i), correct?

3. I am a little bummed by what to put down for my buffer and buffer size,
is
what I did proper?

4. This is not in the code, but if I actually know the value I want in the
memory is
a Double, how do i modify my code to look at only doubles?

thanks all!

> code starts


User32 = ctypes.WinDLL('User32', use_last_error=True)
Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
PROCESS_VM_READ = 0x0010
PID = 'given'


Process = Kernel32.OpenProcess(PROCESS_VM_READ, 0, PID)
ReadProcessMemory = Kernel32.ReadProcessMemory


buffer_size = 1000
buffer = ctypes.create_string_buffer(buffer_size)

# looking for the addresses where the values are -143, for example
# I used 1000 as an abitrary number, I need to scan the entire
application
# memory space, but I am not sure how to acquire that value.

for i in range(1,1000):
if ReadProcessMemory(Process, hex(i), buffer, buffer_size, None):
if float(buffer) == int(-143) :
print(float(buffer))


print('Done.')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to sort float number from big numbers to small numbers?

2017-09-25 Thread edmundo pierre via Tutor
I am trying to sort float numbers input by an user from the bigger to smaller 
number. I do not know how to compare float numbers. Any ideas? Thank you!
Edwin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] logging to cmd.exe

2017-09-25 Thread Albert-Jan Roskam
Hi,


With Python 3.5 under Windows I am using the logging module to log messages to 
stdout (and to a file), but this occasionally causes logging errors because 
some characters cannot be represented in the codepage used by cmd.exe (cp850, 
aka OEM codepage, I think). What is the best way to prevent this from 
happening? The program runs fine, but the error is distracting. I know I can 
use s.encode(sys.stdout.encoding, 'replace') and log that, but this is ugly and 
tedious to do when there are many log messages. I also don't understand why %r 
(instead of %s) still causes an error. I thought that the character 
representation uses only ascii characters?!


import logging
import sys

assert sys.version_info.major > 2
logging.basicConfig(filename="d:/log.txt", 
level=logging.DEBUG,format='%(asctime)s %(message)s')
handler = logging.StreamHandler(stream=sys.stdout)
logger = logging.getLogger(__name__)
logger.addHandler(handler)

s = '\u20ac'
logger.info("euro sign: %r", s)



--- Logging error ---
Traceback (most recent call last):
  File "c:\python3.5\lib\logging\__init__.py", line 982, in emit
stream.write(msg)
  File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 
12: character maps to 
Call stack:
  File "q:\temp\logcheck.py", line 10, in 
logger.info("euro sign: %r", s)
Message: 'euro sign: %r'
Arguments: ('\u20ac',)


Thanks in advance for your replies!


Albert-Jan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to sort float number from big numbers to small numbers?

2017-09-25 Thread Alan Gauld via Tutor

On 25/09/17 04:34, edmundo pierre via Tutor wrote:

I am trying to sort float numbers input by an user from the bigger to smaller 
number. I do not know how to compare float numbers. Any ideas? Thank you!


The same way you sort anything else.
Using the comparison operations
==, <, >, <=, >=

There is a slight snag in that comparing float to absolute values is not 
straightforward but for sorting thats irrelevant.


Of course, normally you just use the sort() method to do it, or call the 
sorted() function.



Alan G

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to sort float number from big numbers to small numbers?

2017-09-25 Thread David Rock
> On Sep 24, 2017, at 22:34, edmundo pierre via Tutor  wrote:
> 
> I am trying to sort float numbers input by an user from the bigger to smaller 
> number. I do not know how to compare float numbers. Any ideas? Thank you!
> Edwin

Some basic ideas to think about:

1. inputs are usually strings, so make sure you are storing them as floats
2. put them in a list.  You can then use list built-ins to sort 

What have you tried so far?  Are you trying to start at all, or are you trying 
things that aren’t working?


— 
David Rock
da...@graniteweb.com




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] logging to cmd.exe

2017-09-25 Thread Peter Otten
Albert-Jan Roskam wrote:

> Hi,
> 
> 
> With Python 3.5 under Windows I am using the logging module to log
> messages to stdout (and to a file), but this occasionally causes logging
> errors because some characters cannot be represented in the codepage used
> by cmd.exe (cp850, aka OEM codepage, I think). What is the best way to
> prevent this from happening? The program runs fine, but the error is
> distracting. I know I can use s.encode(sys.stdout.encoding, 'replace') and
> log that, but this is ugly and tedious to do when there are many log
> messages. I also don't understand why %r (instead of %s) still causes an
> error. I thought that the character representation uses only ascii
> characters?!

Not in Python 3. You can enforce ascii with "%a":

>>> euro = '\u20ac'
>>> print("%r" % euro)
'€'
>>> print("%a" % euro)
'\u20ac'

Or you can set an error handler with PYTHONIOENCODING (I have to use 
something that is not utf-8-encodable for the demo):

$ python3 -c 'print("\udc85")'
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'utf-8' codec can't encode character '\udc85' in 
position 0: surrogates not allowed

$ PYTHONIOENCODING=:backslashreplace python3 -c 'print("\udc85")'
\udc85

Or you follow the convention and log to stderr:

$ python3 -c 'import sys; print("\udc85", file=sys.stderr)'
\udc85
$ $ python3 -c 'import logging; logging.basicConfig(); 
logging.getLogger().warn("\udc85")' > to_prove_it_s_not_stdout
WARNING:root:\udc85

> import logging
> import sys
> 
> assert sys.version_info.major > 2
> logging.basicConfig(filename="d:/log.txt",
> level=logging.DEBUG,format='%(asctime)s %(message)s') handler =
> logging.StreamHandler(stream=sys.stdout) logger =
> logging.getLogger(__name__) logger.addHandler(handler)
> 
> s = '\u20ac'
> logger.info("euro sign: %r", s)
> 
> 
> 
> --- Logging error ---
> Traceback (most recent call last):
>   File "c:\python3.5\lib\logging\__init__.py", line 982, in emit
> stream.write(msg)
>   File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode
> return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
> position 12: character maps to  Call stack:
>   File "q:\temp\logcheck.py", line 10, in 
> logger.info("euro sign: %r", s)
> Message: 'euro sign: %r'
> Arguments: ('\u20ac',)
> 
> 
> Thanks in advance for your replies!
> 
> 
> Albert-Jan
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to sort float number from big numbers to small numbers?

2017-09-25 Thread Steven D'Aprano
On Mon, Sep 25, 2017 at 03:34:05AM +, edmundo pierre via Tutor wrote:

> I am trying to sort float numbers input by an user from the bigger to 
> smaller number. I do not know how to compare float numbers. Any ideas? 
> Thank you!

If the numbers are provided by the user, they will be in the form of 
strings, so you have to convert to float first:

# pretend these came from the user
a = '1.2'
b = '100.9'
c = '2.4'
alist = [a, b, c]
alist.sort()
print(alist)


will print the result:

['1.2', '100.9', '2.4']


which is not what you want. Instead, do this:


# pretend these came from the user
a = '1.2'
b = '100.9'
c = '2.4'
alist = [float(a), float(b), float(c)]
alist.sort()
print(alist)


which will print

[1.2, 2.4, 100.9]

instead.

Also, you can make a copy of the list using sorted() instead of sort():

alist = [float(a), float(b), float(c)]
print(sorted(alist))


Be warned: some numbers which can be written exactly in decimal, like 
0.1, may sometimes appear ever-so-slightly "off" when you convert to a 
float. This is not a bug, but an unfortunate side-effect of the way 
computers do arithmetic in base 2 (binary). Feel free to ask if you 
would like more information.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] logging to cmd.exe

2017-09-25 Thread Mark Lawrence via Tutor

On 25/09/2017 14:20, Albert-Jan Roskam wrote:

Hi,


With Python 3.5 under Windows I am using the logging module to log messages to 
stdout (and to a file), but this occasionally causes logging errors because 
some characters cannot be represented in the codepage used by cmd.exe (cp850, 
aka OEM codepage, I think). What is the best way to prevent this from 
happening? The program runs fine, but the error is distracting. I know I can 
use s.encode(sys.stdout.encoding, 'replace') and log that, but this is ugly and 
tedious to do when there are many log messages. I also don't understand why %r 
(instead of %s) still causes an error. I thought that the character 
representation uses only ascii characters?!


import logging
import sys

assert sys.version_info.major > 2
logging.basicConfig(filename="d:/log.txt", 
level=logging.DEBUG,format='%(asctime)s %(message)s')
handler = logging.StreamHandler(stream=sys.stdout)
logger = logging.getLogger(__name__)
logger.addHandler(handler)

s = '\u20ac'
logger.info("euro sign: %r", s)



--- Logging error ---
Traceback (most recent call last):
   File "c:\python3.5\lib\logging\__init__.py", line 982, in emit
 stream.write(msg)
   File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode
 return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 12: 
character maps to 
Call stack:
   File "q:\temp\logcheck.py", line 10, in 
 logger.info("euro sign: %r", s)
Message: 'euro sign: %r'
Arguments: ('\u20ac',)


Thanks in advance for your replies!


Albert-Jan



Rather than change your code can you change the codepage with the chcp 
command?


C:\Users\Mark\Documents\MyPython>chcp
Active code page: 65001

C:\Users\Mark\Documents\MyPython>type mytest.py
import logging
import sys

assert sys.version_info.major > 2
logging.basicConfig(filename="d:/log.txt", 
level=logging.DEBUG,format='%(asctime)s %(message)s')

handler = logging.StreamHandler(stream=sys.stdout)
logger = logging.getLogger(__name__)
logger.addHandler(handler)

s = '\u20ac'
logger.info("euro sign: %r", s)
C:\Users\Mark\Documents\MyPython>mytest.py
euro sign: '€'
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

---
This email has been checked for viruses by AVG.
http://www.avg.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting a string to a byte array

2017-09-25 Thread Cameron Simpson

On 25Sep2017 09:29, Phil  wrote:
[...]
Just for interest I amended my code to use what you provided and tried it 
under IDLE. There aren't any errors but but my Arduino is not responding.
However, if I enter python3 mycode.py then it works perfectly. I'm sure 
there's an explanation for this.


Just to this. If your serial file handle is a normal buffered one, you may also 
need to call .flush(). When you run from the command line as "python3 
mycode.py" is done automatically at programme exit. In the IDE, the programme 
has not exited, so your bytes may be lurking in the buffer, unsent.


Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting a string to a byte array

2017-09-25 Thread Phil

On 26/09/17 07:02, Cameron Simpson wrote:


Just to this. If your serial file handle is a normal buffered one, you 
may also need to call .flush(). When you run from the command line as 
"python3 mycode.py" is done automatically at programme exit. In the IDE, 
the programme has not exited, so your bytes may be lurking in the 
buffer, unsent.


Thank you Cameron, that sounds like a logical explanation. I'll try it.

--
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor