Pythonic way to print a chess board in console - python

I was wondering what would be the best pythonic way to print a console chess board using a dictionary.
Should I use list ? but what happens if their is no piece on the board on a certain row?
Edit :

In chess programs (typically for computing best moves, solve riddles, etc.) internally a 10×10 board is used where the middle 8×8 fields are used by the visible and playable board. (The margin helps keep the code simple.) To simplify memory management, typically a linear memory is used to store this, i. e. 100 fields. The position of a field of the board is then computed using board[(y+1) * 10 + (x+1)] with x, y in the range 0..7. (Init by board = [ 0 ] * 100.)
So you might want to use this, just in case your program later should expand to also compute moves or stay compatible with a library doing this.
As for graphics, I propose to use this board:
A B C D E F G H
------- ------- ------- ------- ------- ------- ------- -------
| #___# | %~b | .#. | \o*o/ | __+__ | .#. | %~b | #___# |
8| ### | `'dX | ### | ### | `###' | ### | `'dX | ### |8
| d###b | d##b | ./A\. | d###b | d###b | ./A\. | d##b | d###b |
------- ------- ------- ------- ------- ------- ------- -------
| _ | _ | _ | _ | _ | _ | _ | _ |
7| (#) | (#) | (#) | (#) | (#) | (#) | (#) | (#) |7
| d#b | d#b | d#b | d#b | d#b | d#b | d#b | d#b |
------- ------- ------- ------- ------- ------- ------- -------
| | . . . | | . . . | | . . . | | . . . |
6| | . . . | | . . . | | . . . | | . . . |6
| | . . . | | . . . | | . . . | | . . . |
------- ------- ------- ------- ------- ------- ------- -------
| . . . | | . . . | | . . . | | . . . | |
5| . . . | | . . . | | . . . | | . . . | |5
| . . . | | . . . | | . . . | | . . . | |
------- ------- ------- ------- ------- ------- ------- -------
| | . . . | | . . . | | . . . | | . . . |
4| | . . . | | . . . | | . . . | | . . . |4
| | . . . | | . . . | | . . . | | . . . |
------- ------- ------- ------- ------- ------- ------- -------
| . . . | | . . . | | . . . | | . . . | |
3| . . . | | . . . | | . . . | | . . . | |3
| . . . | | . . . | | . . . | | . . . | |
------- ------- ------- ------- ------- ------- ------- -------
| _ | _ | _ | _ | _ | _ | _ | _ |
2| ( ) | ( ) | ( ) | ( ) | ( ) | ( ) | ( ) | ( ) |2
| /_\ | /_\ | /_\ | /_\ | /_\ | /_\ | /_\ | /_\ |
------- ------- ------- ------- ------- ------- ------- -------
| [___] | %~\ | .O. | \o^o/ | __+__ | .O. | %~\ | [___] |
1| [ ] | `')( | \ / | [ ] | `. .' | \ / | `')( | [ ] |1
| /___\ | <__> | /_\ | /___\ | /___\ | /_\ | <__> | /___\ |
------- ------- ------- ------- ------- ------- ------- -------
A B C D E F G H
I made these ASCII graphics back in the 90s for Tubmud. Feel free to use them.

I would use a nested list for that problem. In chess, you have an 8x8 grid, which can easily built by python list comprehension magic.
grid = [ [" "] * 8 for unused in range(8)]
You than can access and alter the elements by indexing an array:
grid[0][0] = 'T'
You can use lower case letters for white and upper case letters for black figures, and their first letter or so for the kind, like 'Q' for black queen.
Dictionary looks not good for me, since it is hard to make operations for checking valid moves or sth, because you have no other way to determine the position of a figure by iterating over all values. The grid is most natural.

Related

Button Not Interacting (Custom Type) - Pywinauto

I am working on automating a process that uses our ancient HRIS system that unfortunately doesn't have API Access.
I am fairly new to Python, so I have been taking this task bit by bit. I've managed to connect to the app and input my username and password to sign in. However, I am stuck on selecting a menu item. I've tried everything that I know to do and have Googled until I've gone cross-eyed.
Dialog - 'City of Conway LIVE Springbrook V7' (L0, T0, R1032, B1039)
['Dialog', 'City of Conway LIVE Springbrook V7Dialog', 'City of Conway LIVE Springbrook V7', 'Dialog0', 'Dialog1']
child_window(title="City of Conway LIVE Springbrook V7", auto_id="MainMenu", control_type="Window")
|
| Pane - '' (L231, T87, R1024, B118)
| ['Pane', 'Pane0', 'Pane1']
| child_window(auto_id="_panelExWorkArea", control_type="Pane")
| |
| | Pane - 'Desktop' (L231, T90, R1021, B115)
| | ['DesktopPane', 'Desktop', 'Pane2']
| | child_window(title="Desktop", auto_id="_ssiGroupHeaderWorkArea", control_type="Pane")
|
| Pane - '' (L228, T87, R231, B1005)
| ['Pane3']
| child_window(auto_id="_ssiExpandableSplitter1", control_type="Pane")
|
| Pane - '' (L8, T87, R228, B1005)
| ['Pane4']
| child_window(auto_id="_panelTaskArea", control_type="Pane")
| |
| | Pane - '' (L11, T90, R228, B1002)
| | ['Pane5']
| | child_window(auto_id="328582", control_type="Pane")
| | |
| | | TreeView - '' (L11, T115, R228, B1002)
| | | ['TreeView', 'TreeView0', 'TreeView1']
| | | child_window(auto_id="1775914", control_type="Tree")
| | | |
| | | | Pane - '' (L28, T269, R194, B609)
| | | | ['Pane6']
| | | | child_window(auto_id="726924", control_type="Pane")
| | | | |
| | | | | Pane - '' (L28, T269, R194, B609)
| | | | | ['Pane7']
| | | | | child_window(auto_id="1317216", control_type="Pane")
| | | | | |
| | | | | | TreeView - '' (L28, T269, R194, B609)
| | | | | | ['TreeView2']
| | | | | | child_window(auto_id="2101028", control_type="Tree")
| | | | | | |
| | | | | | | Custom - 'Maintenance' (L0, T0, R0, B0)
| | | | | | | ['Custom', 'Maintenance', 'MaintenanceCustom', 'Custom0', 'Custom1', 'Maintenance0', 'Maintenance1', 'MaintenanceCustom0', 'MaintenanceCustom1']
| | | | | | | child_window(title="Maintenance", control_type="Custom")
I'm using a few tools to inspect the GUI, and this one specifically allows me to do the desired task by selecting "do it". It allows me to expand and collapse the section, so surely I've got to be missing something somewhere?
enter image description here
enter image description here
Here is my code:
from pywinauto import Application
app=Application(backend="uia").connect(path=r"C:\Users\skywalker\AppData\Local\Apps\2.0\C38DNYDP.PZ6\07BV1NGN.8G6\spri..ons1_b443b3e57637483a_0007.000f_52ec298e739bfebb", timeout = 30)
maintenance = app.CityofConwayLIVESpringbrookV7.WindowsForms10.Window.8.app.0.a0f91b_r8_ad1, 263022
maintenance.click()
I would also like to mention that I CAN get it to work with Click_Input, but I would like to avoid that if at all possible.

How to create a csv file from a folder?

So my folder structure looks like this
MP_Data
|___dir1
| | 0
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| | 1
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| .
| .
| 29
|___dir2
| | 0
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| | 1
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| .
| .
| 29
|___dir3
| | 0
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| | 1
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| .
| .
| 29
So I need csv file for including dataset for creating model for deploying it using flask. The csv file should contain all the data present inside the MP_Data folder. I am super new to flask so I've no clue how to do this, can anyone pls help me?
Quite simple.
Use the following approach (relative to where you've saved your python file).
I'll be assuming you've saved your python file in MP_Data and that your CSV file is in dir1.
import pandas as pd
df = pd.read_csv('dir1/data.csv')
print(df)
I am using this approach!
import pandas as pd
#loading the data
data = pd.read_csv('file:///C:/Users/d/Downloads/Nifty_50_Futures_Historical_Data.csv')
print(data)

Handling Custom ComboBox in pywinauto

I am writing a script to select a region in a ComboBox. I can use app.dialog['Region:ComboBox'].select(index), but not app.dialog['Region:ComboBox'].select('string'). I notice the ComboBox is custom and is generated real time. How can I select the proper option using a string? The string would be a region like US West, US East, etc.
| | GroupBox - 'Preferences' (L811, T456, R1108, B593)
| | ['PreferencesGroupBox', 'Preferences', 'GroupBox', 'GroupBox0', 'GroupBox1']
| | child_window(title="Preferences", control_type="Group")
| | |
| | | Static - 'PREFERENCES' (L817, T462, R1102, B476)
| | | ['PREFERENCES', 'PREFERENCESStatic', 'Static25']
| | | child_window(title="PREFERENCES", control_type="Text")
| | |
| | | Static - 'Region Text:' (L0, T0, R0, B0)
| | | ['Region Text:Static', 'Region Text:', 'Static26']
| | | child_window(title="Region Text:", control_type="Text")
| | |
| | | Static - '' (L0, T0, R0, B0)
| | | ['Static27']
| | |
| | | Static - 'Region:' (L822, T498, R937, B512)
| | | ['Region:Static', 'Region:', 'Static28']
| | | child_window(title="Region:", control_type="Text")
| | |
| | | Custom - '' (L947, T492, R1097, B518)
| | | ['Custom3', 'Region:Custom']
| | | |
| | | | ComboBox - '' (L947, T492, R1097, B518)
| | | | ['ComboBox', 'Region:ComboBox', 'ComboBoxESRI.ArcGIS.Azure.IaaS.Interfaces.RegionInfo']
| | | | |
| | | | | Edit - '' (L0, T0, R0, B0)
| | | | | ['Edit', 'Edit0', 'Edit1']
| | | | | child_window(auto_id="PART_EditableTextBox", control_type="Edit")
| | |
| | | Static - 'Remote Desktop Port:' (L822, T534, R937, B548)
| | | ['Remote Desktop Port:', 'Remote Desktop Port:Static', 'Static29']
| | | child_window(title="Remote Desktop Port:", control_type="Text")
| | |
| | | Edit - '3389' (L947, T528, R1097, B554)
| | | ['Edit2', 'Remote Desktop Port:Edit']
| | | child_window(title="3389", control_type="Edit")
| | | |
| | | | ScrollBar - '' (L0, T0, R0, B0)
| | | | ['ScrollBar', 'ScrollBar0', 'ScrollBar1']
| | | | child_window(auto_id="VerticalScrollBar", control_type="ScrollBar")
| | | |
| | | | ScrollBar - '' (L0, T0, R0, B0)
| | | | ['ScrollBar2']
| | | | child_window(auto_id="HorizontalScrollBar", control_type="ScrollBar")
| | | |
| | | | Button - 'r' (L0, T0, R0, B0)
| | | | ['r', 'Button8', 'rButton']
| | | | child_window(title="r", auto_id="PART_ClearText", control_type="Button")
| | |
| | | CheckBox - 'Track application usage anonymously' (L822, T564, R1097, B582)
| | | ['Track application usage anonymously', 'CheckBox', 'Track application usage anonymouslyCheckBox', 'Track application usage anonymously0', 'Track application usage anonymously1']
| | | child_window(title="Track application usage anonymously", control_type="CheckBox")
| | | |
| | | | Static - 'Track application usage anonymously' (L846, T565, R1043, B581)
| | | | ['Track application usage anonymously2', 'Track application usage anonymouslyStatic', 'Static30']
| | | | child_window(title="Track application usage anonymously", control_type="Text")
Here's how I implemented it. This function emulates the select() function more efficiently.
def comboselect(combo,sel):
combo.type_keys("{ENTER}") # Selects the combo box
texts = combo.texts() #gets all texts available in combo box
try:
index = texts.index(str(sel)) #find index of required selection
except ValueError:
return False
sel_index = combo.selected_index() # find current index of combo
if(index>sel_index):
combo.type_keys("{DOWN}"*abs(index-sel_index))
else:
combo.type_keys("{UP}"*abs(index-sel_index))
return True

Blackjack Game - displaying ASCII Graphics / Multiline Strings

I'm pretty new to Python and currently trying to create a basic blackjack game using ASCII graphics to represent the cards. I've placed the card images in a list of multiline strings and the idea is to call the specific index for each one when a card needs to be displayed.
However I can't get them to print next to each other, and no amount of tinkering seems to get it to work. Here's the code:
CARDS = ['''
-------
|K |
| |
| |
| |
| K|
------- ''', '''
-------
|Q |
| |
| |
| |
| Q|
------- ''', '''
-------
|J |
| |
| |
| |
| J|
------- ''', '''
-------
|10 |
| |
| |
| |
| 10|
------- ''', '''
-------
|9 |
| |
| |
| |
| 9|
------- ''', '''
-------
|8 |
| |
| |
| |
| 8|
------- ''', '''
-------
|7 |
| |
| |
| |
| 7|
------- ''', '''
-------
|6 |
| |
| |
| |
| 6|
------- ''', '''
-------
|5 |
| |
| |
| |
| 5|
------- ''', '''
-------
|6 |
| |
| |
| |
| 6|
------- ''', '''
-------
|5 |
| |
| |
| |
| 5|
------- ''', '''
-------
|4 |
| |
| |
| |
| 4|
------- ''', '''
-------
|3 |
| |
| |
| |
| 3|
------- ''', '''
-------
|2 |
| |
| |
| |
| 2|
------- ''', '''
-------
|A |
| |
| |
| |
| A|
------- '''
]
BLANKCARD = '''
-------
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
------- '''
def displayCards():
print(CARDS[2] + CARDS[14], end='')
displayCards()
The above code prints the following:
-------
|J |
| |
| |
| |
| J|
-------
-------
|A |
| |
| |
| |
| A|
-------
I've tried using end='' to get rid of the new line, but no joy. Does anyone have any suggestions about how I can get the cards next to each other?
Thanks in advance!
Interesting little problem. Here's a quick solution that I whipped up.
class Card:
def topchar(char):
return '|{} |'.format(char)
def botchar(char):
return '| {}|'.format(char)
def print(char_list):
top = ' ------- '
side ='| |'
topout = ''
topchar = ''
botchar = ''
blankside = ''
for char in char_list:
topout += top + ' '
topchar += Card.topchar(char) + ' '
blankside += side + ' '
botchar += Card.botchar(char) + ' '
print(topout)
print(topchar)
print(blankside)
print(blankside)
print(blankside)
print(botchar)
print(topout)
With the way you're doing it, that would be very difficult. When you write
end=''
That only gets rid of the newline at the very end of the printed text. The problem is, every one of your cards has a new line on the right side:
-------
|J |
| |
| | # <--- Newline here
| |
| J|
-------
You would need to create a function that takes a list of cards, and creates one long line of them. That will be a solid project in its own. You would have to take the strings that make up the cards, cut them into lines (cut on each new line), place all the corresponding lines together, getting rid of the newlines in between, then glue all the pieces together. I might actually try this for a morning project now.
And instead of hard coding all the cards like you have, create a function that you give a card value to, and it creates a card for you.
I suggest you write a function that, given i and n, returns a string which represents line i of card n. You can then call that in a double nested loop, printing the results in sequence, to get the result you need.
You can start by making an example of the output you want to see, to use as a reference while coding the loop.

RDF/SKOS concept hierarchy as Python dictionary

In Python, how do I turn RDF/SKOS taxonomy data into a dictionary that represents the concept hierarchy only?
The dictionary must have this format:
{ 'term1': [ 'term2', 'term3'], 'term3': [{'term4' : ['term5', 'term6']}, 'term6']}
I tried using RDFLib with JSON plugins, but did not get the result I want.
I'm not much of a Python user, and I haven't worked with RDFLib, but I just pulled the SKOS and vocabulary from the SKOS vocabularies page. I wasn't sure what concepts (RDFS or OWL classes) were in the vocabulary, nor what their hierarchy was, so I ran this a SPARQL query using Jena's ARQ to select classes and their subclasses. I didn't get any results. (There were classes defined of course, but none had subclasses.) Then I decided to use both the SKOS and SKOS-XL vocabularies, and to ask for properties and subproperties as well as classes and subclasses. This is the SPARQL query I used:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?property ?subproperty ?class ?subclass WHERE {
{ ?subclass rdfs:subClassOf ?class }
UNION
{ ?subproperty rdfs:subPropertyOf ?property }
}
ORDER BY ?class ?property
The results I got were
-------------------------------------------------------------------------------------------------------------------
| property | subproperty | class | subclass |
===================================================================================================================
| rdfs:label | skos:altLabel | | |
| rdfs:label | skos:hiddenLabel | | |
| rdfs:label | skos:prefLabel | | |
| skos:broader | skos:broadMatch | | |
| skos:broaderTransitive | skos:broader | | |
| skos:closeMatch | skos:exactMatch | | |
| skos:inScheme | skos:topConceptOf | | |
| skos:mappingRelation | skos:broadMatch | | |
| skos:mappingRelation | skos:closeMatch | | |
| skos:mappingRelation | skos:narrowMatch | | |
| skos:mappingRelation | skos:relatedMatch | | |
| skos:narrower | skos:narrowMatch | | |
| skos:narrowerTransitive | skos:narrower | | |
| skos:note | skos:changeNote | | |
| skos:note | skos:definition | | |
| skos:note | skos:editorialNote | | |
| skos:note | skos:example | | |
| skos:note | skos:historyNote | | |
| skos:note | skos:scopeNote | | |
| skos:related | skos:relatedMatch | | |
| skos:semanticRelation | skos:broaderTransitive | | |
| skos:semanticRelation | skos:mappingRelation | | |
| skos:semanticRelation | skos:narrowerTransitive | | |
| skos:semanticRelation | skos:related | | |
| | | _:b0 | <http://www.w3.org/2008/05/skos-xl#Label> |
| | | skos:Collection | skos:OrderedCollection |
-------------------------------------------------------------------------------------------------------------------
It looks like there's not much concept hierarchy in SKOS at all. Could that explain why you didn't get the results you wanted before?

Categories

Resources