Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #5652
| From | ruppert@hs-worms.de |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | Re: [Python-de] f-string Formatanweisung |
| Date | 2020-07-13 11:13 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <rehfle$5os$1@dont-email.me> (permalink) |
| References | <rcq1j2$5b5$1@dont-email.me> <3e3c30fe-d11e-6061-2ed4-0a7015359882@behnel.de> <mailman.253.1592831688.24731.python-de@python.org> <Formatierung-20200712232843@ram.dialup.fu-berlin.de> |
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> Stefan Behnel <python-de@behnel.de> writes:
>>möglich, f-strings zu generieren. (Ok, du könntest eval() verwenden, aber
>>das ist recht langsam und auch fehleranfällig ??? tu's lieber nicht.)
>
> ...
>
> import sys
>
> argv = sys.argv
> argv =[ "my/program.py" ]
> argv =[ "my/program.py", "3" ]
>
> def check_usage():
> if len( argv )< 2 :
> a = argv[ 0 ].split( '/' )
> a = a[ -1 ]
> print( 'usage:', a, 'digits of GrayCode' )
> print( ' eg.', a, '12' )
> sys.exit( 1 )
>
> check_usage()
>
> def gray_code_from_binary_code( bin ):
> return bin^( bin >> 1 )
>
> def format_digits( digits ):
> return digits if digits >= 4 else 4
>
> specified_digits = int( argv[ 1 ])
> decimal_digits = format_digits( int( specified_digits / 3.321928 + 1 ))
> gray_digits = format_digits( specified_digits )
> binary_digits = gray_digits
> table_header = f'{"#val":{decimal_digits}s} {"gray":{gray_digits}s} {"bin":{binary_digits}s}'
> for binary_code in range( 2**specified_digits ):
> if binary_code % 24 == 0: print( table_header )
> gray_code = gray_code_from_binary_code( binary_code )
> print( f"{binary_code:0{decimal_digits}d}, {gray_code:0{gray_digits}b}, {binary_code:0{binary_digits}b}" )
>
>
Vielen Dank für das Posting, ich hab' viel daraus gelernt.
Und in mein grayCode.py habe ich die f'-strings bereits
übernommen und es funktioniert. :-)
nochmals vielen Dank
Martin Ruppert
Back to de.comp.lang.python | Previous | Next — Previous in thread | Find similar
f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 10:36 +0000
Re: [Python-de] f-string Formatanweisung Mike Müller <mmueller@python-academy.de> - 2020-06-22 14:27 +0200
Re: [Python-de] f-string Formatanweisung Mike Müller <mmueller@python-academy.de> - 2020-06-22 14:31 +0200
Re: [Python-de] f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 13:22 +0000
Re: [Python-de] f-string Formatanweisung Stefan Behnel <python-de@behnel.de> - 2020-06-22 14:55 +0200
Re: f-string Formatanweisung "Andreas B." <ab@sysing.de> - 2020-06-22 15:03 +0200
Re: f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 13:28 +0000
Re: [Python-de] f-string Formatanweisung Stefan Behnel <python-de@behnel.de> - 2020-06-22 15:14 +0200
Re: [Python-de] f-string Formatanweisung ruppert@hs-worms.de - 2020-06-22 14:03 +0000
Re: [Python-de] f-string Formatanweisung ruppert@hs-worms.de - 2020-07-13 11:13 +0000
csiph-web