seat.py
I developed this minimalist Python script to facilitate some repetitive tasks. Over time, it evolved to include more features and became more streamlined. This script might work as a demonstration of improved software development conventions, regarding how to enhance functionality and maintain simplicity.
Manual
This seat generator allows random generation of seats from a set of names with various modifications, including but not limited to random assortment, fixed seat, preferred seating areas, seat mate with or without preference, and rotate options.
All presets for generating a seat pattern is controlled by a config.json file. Below is an example of config.json. Such a file contains six entries of settings. The tabs row and col determine the size of the rectangular room. noSit stands for the positions that are not available for sitting, such as aisles between seats. nameList stands for names to be randomly assigned into seats. seed represents the seed for generation; a same seed with the same configuration always produces the same result. modif is a list of modifications done to the seat pattern.
{
"col": 11,
"modif": [
{
"mate": "赵天宇",
"mode": "pref",
"name": "李欣怡"
},
{
"mode": "pref",
"name": "王子涵",
"mate": "陈浩然"
},
{
"mate": "林雨辰",
"mode": "pref",
"name": "周嘉欣",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "all"
},
"mode": "add",
"row": {
"mode": "scale",
"value": [0.7, 1]
}
}
]
}
},
{
"mate": "吴梓轩",
"mode": "pref",
"name": "黄雅琳"
},
{
"mode": "pref",
"name": "刘云飞",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "all"
},
"mode": "add",
"row": {
"mode": "scale",
"value": [0, 0.3]
}
}
]
}
},
{
"mode": "pref",
"name": "张伟强",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "all"
},
"mode": "add",
"row": {
"mode": "scale",
"value": [0.7, 1]
}
}
]
}
},
{
"mode": "rotate",
"names": ["赖泽民", "刘星宇", "杨嘉诚"]
},
{
"mode": "rotate",
"names": ["陈欣怡", "黄彦妮", "林慧"]
},
{
"mode": "pref",
"name": "李志强",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "range",
"value": [3, 11]
},
"mode": "add",
"row": {
"mode": "range",
"value": [0, 3]
}
}
]
}
},
{
"mode": "pref",
"name": "钟海涛",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "all"
},
"mode": "add",
"row": {
"mode": "range",
"value": [0, 1]
}
}
]
}
},
{
"mode": "pref",
"name": "朱炜杰",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "range",
"value": [5, 11]
},
"mode": "add",
"row": {
"mode": "range",
"value": [1, 3]
}
}
]
}
},
{
"mate": "许泽",
"mode": "pref",
"name": "李嘉豪",
"pref": {
"mode": "rules",
"rules": [
{
"col": {
"mode": "all"
},
"row": {
"mode": "scale",
"value": [0.99, 1]
},
"mode": "add"
}
]
}
}
],
"nameList": [
"钟海涛",
"周嘉欣",
"李嘉豪",
"赵天宇",
"李欣怡",
"林慧",
"李志强",
"朱炜杰",
"赖泽民",
"吴昊天",
"杨嘉诚",
"王子涵",
"林雨辰",
"张泽",
"陈炜",
"刘云飞",
"陈欣怡",
"黄彦妮",
"周涵",
"赵晨",
"陈浩然",
"刘星宇",
"张伟强",
"赵宇",
"吴梓轩",
"许泽",
"黄雅琳",
"陈静",
"李石",
"张泓"
],
"noSit": {
"rules": [
{
"col": {
"mode": "direct",
"value": [0, 1]
},
"mode": "add",
"row": {
"mode": "range",
"value": [0, 1]
}
},
{
"col": {
"mode": "direct",
"value": [2, 5, 8]
},
"mode": "add",
"row": {
"mode": "range",
"value": [0, 4, 1]
}
}
],
"mode": "rules"
},
"row": 4,
"seed": 20220925
}
It's notable that all five entries have their structures; inappropriate writing of the json file will result in error.
row,col, andseedshould be integers.seedset to0will let the program determine a random seed.nameListshould be a list of strings. Each entry of the list will be a name that appears in the result. A same name appearing twice or more than twice is not supported.noSitdetermines the coordinates that cannot be sit. This tag is a coordinate provider, which must contain amode.- If
modeis set toempty, then no other configuration is need, and the program will regard it as an emptynoSitlist. - If
modeissimple, then add avaluelist where you should put the specific list ofnoSitcoordinates within. For example,"noSit": {"mode": "simple", "value": [[0, 1], [0, 2], [1, 2], [2, 3]]}will add(0, 1),(0, 2),(1, 2), and(2, 3)into thenoSitlist. - If
modeisrules, then a list of rules, calledrules, should be placed under the dict tab. Each rule should be a dict tab that contains three entries:mode,col, androw.modeunder a rule should be eitheradd,del, orinvert.addadds the corresponding positions to thenoSitlist, whiledeldoes the opposite;invertinverts all coordinates (which is to exclude all the existing coordinates in the list and include all the remaining ones).colandrowspecify which array of coordinates should be added. Each of the two should be a dict tab withmodeandvalue.modesupportsdirect,range,all, andscale.directis followed by an integer or a list of integers;rangeis the same as python'srange()function;alldoes not need a specificvalue, but will cover all possible coordinates;scaleis followed by two float numbers between0.0and1.0, indicating the multiplier on the maximum coordinate. For example,"noSit": {"config": {"rules": [{"col": {"mode": "direct", "value": 0}, "mode": "add", "row": {"mode": "range", "value": [0, 2]}}]}}will put(0, 0)and(0, 1)into the list.
- If
modifis a list of modifications that will be done to the random assignment of seats. Different kinds of modification accept different parameters, but all contain amodetag in common.modeshould bemate,fixed,pref, androtate.prefworks to assigned a preffered seating area or a mate for a name. If amateis provided, then the name in the mate will appear by either side of the name. Theprefspace is a coordinate provider, which should act the same asnoSit's.rotateis a special seating option that selects a pair from a list of names according to current week number. Simply provide anameslist under the modification and add as many names into it.fixedfixes a name on a seat.fixedtag has a higher priority than all other modifications, so this mode should be preferred when applicable. The coordinate should be given under thepostag as a list of two numbers.
config.json should be placed under a folder with custom name under the 'save' folder. The name will be used for naming the generated seat file. The program supports generating .txt and .xlsx file. To get a .xlsx file, a template named template.xlsx should also be placed under the folder. \$ ROW and \$ COL specify the starting coordinate where the seat pattern will be assigned into the form. % DAY MONTH YEAR will let the program print the date of the day the seat is generated. Both modifications are optional.
