I'm trying to print the following string in a centralised position in the console, but .center() doesn't seem to work
""" , ,
(.-""-.)
|\ \/ \/ /|
| \ / =. .= \ / |
\( \ o\/o / )/
\_, '-/ \-' ,_/
v \__/ v
\ \__/\__/ /
___\ \|--|/ /___
/` \ / `\
"""
The problem is that in your input string you have spaces before your pixel art.
I used also the answer provided in the comments (link).
import shutil
SIZE = shutil.get_terminal_size()
COLUMNS = SIZE.columns
def tty_center_str(s: str):
print(s.center(COLUMNS))
def tty_center_multiline(s: str):
for line in s.splitlines():
tty_center_str(line.strip())
Output:
>>> tty_center_multiline(s)
, ,
(.-""-.)
|\ \/ \/ /|
| \ / =. .= \ / |
\( \ o\/o / )/
\_, '-/ \-' ,_/
v \__/ v
\ \__/\__/ /
___\ \|--|/ /___
/` \ / `\
I am a beginner in Python and would really appreciate if someone could help me with the following:
I would like to run this script 10 times and for that change for every run the sub-batch (from 0-9):
E.g. the first run would be:
python $GWAS_TOOLS/gwas_summary_imputation.py \
-by_region_file $DATA/eur_ld.bed.gz \
-gwas_file $OUTPUT/harmonized_gwas/CARDIoGRAM_C4D_CAD_ADDITIVE.txt.gz \
-parquet_genotype $DATA/reference_panel_1000G/chr1.variants.parquet \
-parquet_genotype_metadata $DATA/reference_panel_1000G/variant_metadata.parquet \
-window 100000 \
-parsimony 7 \
-chromosome 1 \
-regularization 0.1 \
-frequency_filter 0.01 \
-sub_batches 10 \
-sub_batch 0 \
--standardise_dosages \
-output $OUTPUT/summary_imputation_1000G/CARDIoGRAM_C4D_CAD_ADDITIVE_chr1_sb0_reg0.1_ff0.01_by_region.txt.gz
The second run would be
python $GWAS_TOOLS/gwas_summary_imputation.py \
-by_region_file $DATA/eur_ld.bed.gz \
-gwas_file $OUTPUT/harmonized_gwas/CARDIoGRAM_C4D_CAD_ADDITIVE.txt.gz \
-parquet_genotype $DATA/reference_panel_1000G/chr1.variants.parquet \
-parquet_genotype_metadata $DATA/reference_panel_1000G/variant_metadata.parquet \
-window 100000 \
-parsimony 7 \
-chromosome 1 \
-regularization 0.1 \
-frequency_filter 0.01 \
-sub_batches 10 \
-sub_batch 1 \
--standardise_dosages \
-output $OUTPUT/summary_imputation_1000G/CARDIoGRAM_C4D_CAD_ADDITIVE_chr1_sb0_reg0.1_ff0.01_by_region.txt.gz
I am sure this can be done with a loop but not quite sure how to do it in python?
Thank you so much for any advice,
Sally
While we can't show you how to retrofit a loop to the python code without actually seeing the python code, you could just use a shell loop to accomplish what you want without touching the python code.
For bash shell, it would look like this:
for sub_batch in {0..9}; do \
python $GWAS_TOOLS/gwas_summary_imputation.py \
-by_region_file $DATA/eur_ld.bed.gz \
-gwas_file $OUTPUT/harmonized_gwas/CARDIoGRAM_C4D_CAD_ADDITIVE.txt.gz \
-parquet_genotype $DATA/reference_panel_1000G/chr1.variants.parquet \
-parquet_genotype_metadata
$DATA/reference_panel_1000G/variant_metadata.parquet \
-window 100000 \
-parsimony 7 \
-chromosome 1 \
-regularization 0.1 \
-frequency_filter 0.01 \
-sub_batches 10 \
-sub_batch $sub_batch \
--standardise_dosages \
-output $OUTPUT/summary_imputation_1000G/CARDIoGRAM_C4D_CAD_ADDITIVE_chr1_sb0_reg0.1_ff0.01_by_region.txt.gz
done
What you're looking for seems to be a way to run the same command on the command line a set number of times.
If you're on Linux using the bash shell, this can be done using a shell loop:
for i in {0..9}; do
python $GWAS_TOOLS/gwas_summary_imputation.py \
-by_region_file $DATA/eur_ld.bed.gz \
-gwas_file $OUTPUT/harmonized_gwas/CARDIoGRAM_C4D_CAD_ADDITIVE.txt.gz \
-parquet_genotype $DATA/reference_panel_1000G/chr1.variants.parquet \
-parquet_genotype_metadata $DATA/reference_panel_1000G/variant_metadata.parquet \
-window 100000 \
-parsimony 7 \
-chromosome 1 \
-regularization 0.1 \
-frequency_filter 0.01 \
-sub_batches 10 \
-sub_batch $i \
--standardise_dosages \
-output $OUTPUT/summary_imputation_1000G/CARDIoGRAM_C4D_CAD_ADDITIVE_chr1_sb0_reg0.1_ff0.01_by_region.txt.gz
done
If you're on Windows, similar can be achieved using powershell:
for ($i=0; $i -le 9; $i++) {
python $GWAS_TOOLS/gwas_summary_imputation.py \
-by_region_file $DATA/eur_ld.bed.gz \
-gwas_file $OUTPUT/harmonized_gwas/CARDIoGRAM_C4D_CAD_ADDITIVE.txt.gz \
-parquet_genotype $DATA/reference_panel_1000G/chr1.variants.parquet \
-parquet_genotype_metadata $DATA/reference_panel_1000G/variant_metadata.parquet \
-window 100000 \
-parsimony 7 \
-chromosome 1 \
-regularization 0.1 \
-frequency_filter 0.01 \
-sub_batches 10 \
-sub_batch $i \
--standardise_dosages \
-output $OUTPUT/summary_imputation_1000G/CARDIoGRAM_C4D_CAD_ADDITIVE_chr1_sb0_reg0.1_ff0.01_by_region.txt.gz
}
a loop in python from 0 to 10 is very easy.
for i in range(0, 10):
do stuff
So, I'm pretty new to python, but I'm currently coding a text-adventure game. I was wondering how I would be able to display the text at the bottom of the screen while an ASCII image is displayed on top, just like SanctuaryRPG.
Alright, so basically, you want to print an ASCII image with text. This shouldn't be too difficult.
Firstly, set the ASCII image to a variable. For example:
img = '''
__,__
.--. .-" "-. .--.
/ .. \/ .-. .-. \/ .. \
| | '| / Y \ |' | |
| \ \ \ 0 | 0 / / / |
\ '- ,\.-"`` ``"-./, -' /
`'-' /_ ^ ^ _\ '-'`
.--'| \._ _./ |'--.
/` \ \ `~` / / `\
/ '._ '---' _.' \
/ '~---~' | \
/ _. \ \
/ .'-./`/ .'~'-.|\ \
/ / `\: / `\'. \
/ | ; | '.`; /
\ \ ; \ \/ /
'. \ ; \ \ ` /
'._'. \ '. | ;/_
/__> '. \_ _ _/ , '--.
.' '. .-~~~~~-. / |--'`~~-. \
// / .---'/ .-~~-._/ / / /---..__.' /
((_(_/ / / (_(_(_(---.__ .'
| | _ `~~`
| | \'.
\ '....' |
'.,___.'
'''
Next, set the text you want to print out to another variable.
text = "Do you want to say hi to the monkey?"
Finally, print the two like this:
print(img)
print(text)
OR:
print(img + "\n\n" + text)
\n Just means a new line.
To do the same as in SanctuaryRPG with Pygame, you need to use a font where each character is the same width (e.g. Courier):
font = pygame.font.SysFont("Courier", text_height)
Split the text into lines with splitlines() (see How to split a python string on new line characters):
img_text = img.splitlines()
Render the text line by line in a loop:
for i, line in enumerate(img_text):
text_surf = font.render(line, True, (255, 255, 0))
window.blit(text_surf, (50, 20 + i * text_height))
Use the following function:
def renderTextImage(surf, font, text, x, y, color):
img_text = text.splitlines()
for i, line in enumerate(img_text):
text_surf = font.render(line, True, color)
surf.blit(text_surf, (x, y + i * text_height))
Minimal example:
repl.it/#Rabbid76/PyGame-AsciiTextImage
import pygame
img_text = r'''
__,__
.--. .-" "-. .--.
/ .. \/ .-. .-. \/ .. \
| | '| / Y \ |' | |
| \ \ \ 0 | 0 / / / |
\ '- ,\.-"`` ``"-./, -' /
`'-' /_ ^ ^ _\ '-'`
.--'| \._ _./ |'--.
/` \ \ `~` / / `\
/ '._ '---' _.' \
/ '~---~' | \
/ _. \ \
/ .'-./`/ .'~'-.|\ \
/ / `\: / `\'. \
/ | ; | '.`; /
\ \ ; \ \/ /
'. \ ; \ \ ` /
'._'. \ '. | ;/_
/__> '. \_ _ _/ , '--.
.' '. .-~~~~~-. / |--'`~~-. \
// / .---'/ .-~~-._/ / / /---..__.' /
((_(_/ / / (_(_(_(---.__ .'
| | _ `~~`
| | \'.
\ '....' |
'.,___.'
'''
def renderTextImage(surf, font, text, x, y, color):
img_text = text.splitlines()
for i, line in enumerate(img_text):
text_surf = font.render(line, True, color)
surf.blit(text_surf, (x, y + i * text_height))
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
text_height = 16
font = pygame.font.SysFont("Courier", text_height)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
renderTextImage(window, font, img_text, 50, 20, (255, 255, 0))
pygame.display.flip()
pygame.quit()
exit()
I have a bytearray, and when I list the array, I get the following data: (b'v10 \ xc73 \ x9a & \ x9edv \ x19 \ xc3B \ xbf \ x95 \ xc8 \ xd8 \ x9dN \ x8f \ xe9 \ x90J \ xax> r1 \ x1d \ xa7 \ x1fU \ x90 \ XE2 (| p \ XF1 \ x02 \ xbdw \ XB8 \ xb9 \ xf3 \ x0e \ xb2n \ xc7 ',).
And I need to decrypt this data. But the decryption function only receives data, for example, b'v10 \ xc73 \ x9a & \ x9edv \ x19 \ xc3B \ xbf \ x95 \ xc8 \ xd8 \ x9dN \ x8f \ xe9 \ x90J \ xax> r1 \ x1d \ xa7 \ x1fU \ x90 \ xe2 (| p \ xf1 \ x02 \ xbdw \ xb8 \ xb9 \ xf3 \ x0e \ xb2n \ xc7' without () and ,
What can I do?
Supposing we have
data = (b'foo',)
then this data is not a bytearray, nor is it a bytes object:
>>> type(data)
<class 'tuple'>
Because it is a tuple, we may extract that element:
>>> data[0]
b'foo'
>>> type(data[0])
<class 'bytes'>
How would you print a tree in python so it looks like the following:
/\
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
The height should be changeable.
def tree(n):
treeStr = ""
for i in range(0,n):
level = " "*(n-i) + "/" + " "*(2*i) + "\\"
treeStr += level.center(n+1) + "\n"
print(treeStr)
tree(10);