Skip to content

Commit

Permalink
Add 'python-experiments/' from commit '23fef38879b5506c943e59ca2ab26f…
Browse files Browse the repository at this point in the history
…54f3c7b4ec'

git-subtree-dir: python-experiments
git-subtree-mainline: 79a8246
git-subtree-split: 23fef38
  • Loading branch information
generic-github-user committed Aug 30, 2022
2 parents 79a8246 + 23fef38 commit d7ccb72
Show file tree
Hide file tree
Showing 11 changed files with 861 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python-experiments/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
1 change: 1 addition & 0 deletions python-experiments/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*checkpoint*
59 changes: 59 additions & 0 deletions python-experiments/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Copyright ©2021, generic-github-user

This License delineates the terms of use applying to third-party use or modification of the Author's intellectual property and creative works.

## Summary

The following is a simplified and non-exhaustive overview of the terms of this document (the License) in plain English, written to be easily understood. Reviewing the License in its entirety before using this project or creating derivative works from it is encouraged.

You may:
- Adapt the Software and create derivative works (e.g., "forks") according to the rules described in this License
- Use the Software as a whole or its components to create personal projects and/or integrate aspects of its functionality into your projects, both private and public (with attribution)
- Contribute to the Software at the discretion of the original Author

You may not:
- State or imply that the Software's original Author/Creator is aware of or endorses your work (i.e., works based on the Software)
- Make money from the Software/Project or works deriving from it
- Include offensive or discriminatory content in projects or content based on the Software

## Definitions

### Software

The Software shall refer to this GitHub repository, including but not limited to:
- The source code, documentation, and other files and content contained in its main directory and all sub-directories
- For instance, `.txt`, `.md`, `.py`, `.json`, `.html`, and `.js` files, as well as others
- Git version tracking commits reflected in the public GitHub repository
- Associated metadata and supplementary content, including issues (feature requests, bug reports, etc.) posted to the GitHub issue tracker associated with this repository

The Project shall be synonymous with the Software unless stated otherwise.

### Creator

The Creator of the Software shall henceforth refer to GitHub user [generic-github-user](https://github.com/generic-github-user), who is established as the primary designer, architect, engineer, and maintainer of the Software unless one or more of these roles (or other roles) are explicitly relinquished in writing and delegated to another person or organization.

The Creator shall be synonymous with the Author unless stated otherwise.

### User

The User shall refer to any third party, including but not limited to people and organizations *other than the Creator*:
- Who use the Project
- Who create derivative works based on the Project

## Terms

The following terms are imposed for the reuse of the Software.

Any derivative works produced from the original Project must adhere to the following conditions; that is, a User (third-party entity) who creates derivative works must observe and respect these restrictions:
- The derivative work must include Attribution: a visible (i.e., prominent or reasonably expected to be seen by all viewers of the work) and explicit reference (i.e., "due credit") to the original Creator of the Software, with at minimum the Creator's name and an active link to their GitHub profile
- This reference should clearly indicate that the Creator expended significant work, effort, and/or time to develop the original Project and that the existence of the derivative work is possible owing to the existence of the Project and the Creator's investment in it
- The derivative work must include a visible and explicit notice that the work is in no way connected with (beyond implicit status as a derivative work of their work), endorsed by, utilized by, or approved by the Creator
- If possible, the derivative work should include the name of the original Project repository and/or a functioning link to said repository
- The above conditions may be combined into a single statement, for example the following:
> Original project developed by [generic-github-user](https://github.com/generic-github-user), who does not endorse or contribute to our use or modification of the Software.
- The derivative work must not be monetized or used for profit in any way
- The derivative work must not include any offensive material whatsoever, including but not limited to:
- Malware, computer viruses or worms, or other harmful software or content intended to damage computer systems or data, or to acquire data that a User cannot access rightfully and legally
- Images, text, or other data of a violent, sexual, or otherwise indecent nature that violates common professional courtesy, to be evaluated at the discretion of the Creator
- Discriminatory content that explicitly or implicitly contains intent to exclude, demean, slander, provoke, or otherwise harm any individual or group on basis of race, ethnicity, religious affiliation, political views, disability status, sexual and/or romantic preference or orientation, or gender identity
2 changes: 2 additions & 0 deletions python-experiments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# python-experiments
Exploring some experimental tweaks to & novel ways of using the Python programming language
96 changes: 96 additions & 0 deletions python-experiments/automacro.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 57,
"id": "8fcfd188-cd0b-45a9-ade9-b56065eb83d7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Testing 729 operations across 4 examples\n",
"ML True\n",
"ML True\n",
"ML True\n",
"Checked 258 combinations and found 3 matches\n",
"[(<method 'upper' of 'str' objects>, <method 'split' of 'str' objects>, <function <lambda> at 0x000001AF73841310>), (<method 'split' of 'str' objects>, <function <lambda> at 0x000001AF73841310>, <method 'upper' of 'str' objects>), (<method 'split' of 'str' objects>, <method 'upper' of 'str' objects>, <function <lambda> at 0x000001AF73841310>)]\n"
]
}
],
"source": [
"import itertools\n",
"\n",
"examples = [\n",
" ['United States', 'US'],\n",
" ['Continuous Integration', 'CI'],\n",
" ['artificial intelligence', 'AI'],\n",
" ['Machine learning', 'ML']\n",
"]\n",
"manipulations = [\n",
" [lambda x: x[0], 'First char'],\n",
" [lambda x: x[::-1], 'Reverse'],\n",
" [str.lower, 'Lowercase'],\n",
" [str.upper, 'Capitalize'],\n",
" [str.split, 'Split'],\n",
" [lambda x: ''.join(x), 'Join'],\n",
"]\n",
"M = [m[0] for m in manipulations]\n",
"options = []\n",
"max_ops = 3\n",
"print(f'Testing {max_ops**len(manipulations)} operations across {len(examples)} examples')\n",
"checked = 0\n",
"for n in range(max_ops):\n",
" for i in itertools.product(M, repeat=n+1):\n",
" # sequence = []\n",
" for ex in examples:\n",
" # print('Testing sample {} -> {}'.format(*ex))\n",
" S = ex[0]\n",
" for j in i:\n",
" if type(S) is str:\n",
" S = j(S)\n",
" elif type(S) in [list, tuple]:\n",
" S = list(map(j, S))\n",
" if type(S[0]) is list:\n",
" S = list(itertools.chain(*S))\n",
" else:\n",
" print(type(S))\n",
"\n",
" if type(S) is list:\n",
" S = ''.join(S)\n",
" if S != ex[1]:\n",
" # print(S)\n",
" break\n",
" else:\n",
" print(S, True)\n",
" options.append(i)\n",
" checked += 1\n",
" \n",
"print(f'Checked {checked} combinations and found {len(options)} matches')\n",
"print(options)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
81 changes: 81 additions & 0 deletions python-experiments/automacro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
# coding: utf-8

# In[57]:


import itertools

examples = [
# ['United States', 'US'],
# ['Continuous Integration', 'CI'],
# ['artificial intelligence', 'AI'],
# ['Machine learning', 'ML']
['First Last', 'Last, First'],
# ['First Middle Last', 'Last, First Middle'],
]
manipulations = [
[lambda x: x[0], 'First char'],
[lambda x: x[::-1], 'Reverse', True],
[str.lower, 'Lowercase'],
[str.upper, 'Capitalize'],
[str.split, 'Split'],
[lambda x: ', '.join(x), '', True]
]
M = [m[0] for m in manipulations]
options = []
max_ops = 4
print(f'Testing {max_ops**len(manipulations)} operations across {len(examples)} examples')
checked = 0
limit = 200000
for n in range(max_ops):
print((n+1)**len(manipulations))
for i in itertools.product(manipulations, repeat=n+1):
# sequence = []
checked += 1
if checked > limit:
break
for ex in examples:
# print('Testing sample {} -> {}'.format(*ex))
S = ex[0]
for j in i:
# print(S, len(j))
f = j[0]
if type(S) is str:
S = f(S)
elif type(S) in [list, tuple]:
if len(j) < 3:
S = list(map(f, S))
elif j[-1] == True:
S = f(S)

if type(S[0]) is list:
S = list(itertools.chain(*S))
else:
print(type(S))

if type(S) is list:
S = ''.join(S)
if S != ex[1]:
# print(S)
break
else:
print(S, True)
options.append(i)

print(f'Checked {checked} combinations and found {len(options)} matches')
print(options)
# TODO: add heuristics (e.g., scoring intermediate results based on desirable properties of the generated string)


# In[111]:


manipulations[8][0](list('test'))


# In[ ]:




87 changes: 87 additions & 0 deletions python-experiments/processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python
# coding: utf-8

# In[2]:


import ast
import re
import string
import time


# This is a simple Python preprocessor designed to improve concision when using some common operators and functions. It parses the file with a series of regular expressions and replaces abbreviations and various syntactical sugar with their normal Python formats. The tweaks are mostly centered around data access and operations involving higher-order functions.

# In[91]:


sample = """
[52]**3
[6]***4
xy::xy+5
5~[3,3,3]
4:90
1:100:5
dictionary..key
a..b..c
F$G
A$B$C$x
F^6(x)
C ++
[[1, 2], [3, 4]][0, 0]
[1, 2, 3]?
[1,2,3]?
[4 5 6 7 8]
1??1000
N = 20
M = 30
Q = NM
T = 40
T = (N)(Q)
A...Q?
3...7
"""
# a...c?

# (20??100) ?? (200??1000)
# """
types = {
'NAT': r'(\d+)',
'NUM': r'([+-]?\d+\.?\d*)',
'DIV': r'(\,?[ ]*)',
'LIST': r'(\[ (?: VAL DIV?){1,}? \])',
'TUPLE': r'(\((?:NUM(?:,+|\s?)){1,}\))',
'NAME': r'([a-zA-Z]+)',
'EXPR': r'(\((?:OP|NAME|VAL)+?\))',
'OP': r'((?:NAME|VAL)(?:[\+\-\*/] | \?{2})(?:NAME|VAL))',
'VAL': r'(NAT|NUM)',
'LETTER': r'([a-zA-Z])',
'LRANGE': r'(LETTER)[.]{3}(LETTER)',
'STRING': r'(\"LETTER+?\")',
# 'COM': '(#.*\n)'
}
for G in range(3):
for a, b in types.items():
for c, d in types.items():
# print(types[a] == b, types[a], b)
types[a] = types[a].replace(c, '(?:'+d[1:])

for n, m in types.items():
print(n, m)
# print('NUM' in b)

def iterate(f, g, n):
h = '{}({})'.format(f, g)
for i in range(n):
h = '{}({})'.format(f, h)
return h

def tile(n, m):
print(n, m)
s = f'[{n}]'
for i in ast.literal_eval(m):
s = f'[{s} * {i}]'
return s
24 changes: 24 additions & 0 deletions python-experiments/py2english.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
# coding: utf-8

# # py2english

# This program parses a Python program into an abstract syntax tree, then generates a natural language description of what it does and how it does it. This is just an experiment, but if it works well as a proof-of-concept the system could be refined and aid in understanding and analyzing large codebases.

# In[1]:


import ast


# In[2]:


sample_code = """
for i in range(2, 50):
for j in range(2, round(i**(1/2))):
if i % j == 0:
break
else:
print(i)
"""
5 changes: 5 additions & 0 deletions python-experiments/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Automatically generated by https://github.com/damnever/pigar.

# C:\Users\diamo\Desktop\python-projects\python-experiments\.ipynb_checkpoints\time-complexity-checkpoint.ipynb: 3
# C:\Users\diamo\Desktop\python-projects\python-experiments\time-complexity.ipynb: 3
ipython == 7.23.1
Loading

0 comments on commit d7ccb72

Please sign in to comment.