Skip to content
Permalink
Browse files

First major public commit of ExPhil code. Let's do this!

  • Loading branch information...
elimisteve committed Nov 15, 2015
0 parents commit a5766117c69b8a8fe1fea5675e4df1ddb6152689
@@ -0,0 +1,2 @@
*~
*.pyc
@@ -0,0 +1,3 @@
from _attr import *
from base_classes import *
from _inspect import *
102 _attr.py
@@ -0,0 +1,102 @@
#!/usr/bin/env python
# Steven Phillips / elimisteve
# 2015.04.24


def get_attr(dict_or_obj, *attrs):
ATTR_NOT_FOUND = KeyError("{} doesn't have attr(s) {}".format(dict_or_obj, attrs))

if isinstance(dict_or_obj, dict):
for attr in attrs:
if attr in dict_or_obj:
return dict_or_obj[attr], attr
raise ATTR_NOT_FOUND

for attr in attrs:
if hasattr(dict_or_obj, attr):
return getattr(dict_or_obj, attr), attr

raise ATTR_NOT_FOUND


def has_attr(dict_or_obj, attr):
if isinstance(dict_or_obj, dict):
return attr in dict_or_obj

# Assuming it's an object
return hasattr(dict_or_obj, attr)


def has_method(obj, method_name):
# If the attribute is callable, it's a method, otherwise it's a
# property

return hasattr(obj, method_name) and callable(getattr(obj, method_name))


def update_object(obj, kwargs):
for k in kwargs:
setattr(obj, k, kwargs[k])


collection_types = (list, tuple, set, frozenset)

def is_collection(coll):
return any([ isinstance(coll, typ) for typ in collection_types ])


def Is(obj, attr_value):
'''
Meant to capture the following senses of 'is':
- Snow is white, George W. Bush is Republican, this desk is wood,
etc (object `obj` has some attribute `a`, or has an attribute
with a value of `a`.)
- Santa Claus is Santa Claus, 2 + 2 is 4, etc (identity)
Not done yet:
- 2 is even (search for an `is_even` function and see if is_even(2) == True)
'''
#
# TODO(elimisteve): Consider leaving this notion of 'is' out
#

# if type(obj) == type(attr_value):
# return obj is attr_value

# TODO(elimisteve): Questionable. Should string comparisons work?
if isinstance(obj, str) and isinstance(attr_value, str):
return obj is attr_value

# If they're not both strings, assume they're both objects and compare
if type(obj) != str and type(attr_value) != str:
# TODO(elimisteve): Could use `==` instead of `is` here, especially
# to make dict comparisons match... but it's not clear that
# that would be the right behavior; shouldn't a custom __eq__
# method be defined in that case, a custom equals() method
# function, or similar?
return obj is attr_value


assert type(obj) != str
assert type(attr_value) == str

# Check for `obj.something == attr_value`
matches_value = any([ v == attr_value or is_collection(v) and attr_value in v
for v in obj.__dict__.values() ])
if matches_value:
return True

# Check for `obj.attr_value is True`
if has_attr(obj, attr_value) and get_attr(obj, attr_value)[0] is True:
return True

return False


# Does(some_object, 'exists') is probably less awkward than
# Is(some_object, 'exists'). Maybe.
##Does = Is
@@ -0,0 +1,11 @@
#!/usr/bin/env python
# Steven Phillips / elimisteve
# 2015.07.15

import inspect

def instances_in_module(cls, module):
return [instance for name, instance in inspect.getmembers(module, lambda inst: isinstance(inst, cls))]

def vars_by_name_in_module(predicate, module):
return [instance for name, instance in inspect.getmembers(module) if predicate(name)]
@@ -0,0 +1,11 @@
#!/usr/bin/env python
# Steven Phillips / elimisteve
# 2015.06.12

# TODO(elimisteve): Never statically define synonyms like this ever again.
# This should be a database lookup.
OVERWEIGHT = (
'overweight',
'fat',
'chubby',
)
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# Steven Phillips / elimisteve
# 2015.04.24

# Note(elimisteve): This is just an example. The keys/attribute
# names/property names contained in constants such as `TEMPERATURE`
# should only be included in this directory if said keys occur in
# published Executable Philosophy. If you plan to use them in future
# publications, do not use them ahead of time; add them concurrently
# with publication.

# EXAMPLE
TEMPERATURE = (
'temperature', # TODO(elimisteve): Should 'temperature' be allowed? It's
# ambiguous, but will be useful to contextualists.
'temp_f', 'temperature_f',
'temp_c', 'temperature_c',
'temp_k', 'temperature_k',
)
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# Steven Phillips / elimisteve
# 2015.06.09

class ExPhil(object):
'''
Every ExPhil Object, Theory, ThoughtExperiment, etc -- probably
every class defined in any ExPhil argument -- should inherit from
this class (ExPhil).
'''

def __new__(cls, *args, **kwargs):
obj = object.__new__(cls, *args, **kwargs)

# TODO(elimisteve): Consider making this a custom dict-based type that
# warns us when replacing an existing key's value
obj.exphil_attrs = {}

return obj


class Object(ExPhil):
'''
Every object posited by any argument should inherit from this
class (Object).
'''

def __new__(cls, *args, **kwargs):
obj = ExPhil.__new__(cls, *args, **kwargs)
obj.exphil_attrs.update({
'type': 'Object',
})
return obj


class Theory(Object): # TODO(elimisteve): Inherit from Object, or no?
'''
Every (translation of a) philosophical theory defined should
inherit from this class (Theory).
'''

def __new__(cls, *args, **kwargs):
obj = Object.__new__(cls, *args, **kwargs)
obj.exphil_attrs.update({
'type': 'Theory',
})
obj.theory = {}
return obj


class ThoughtExperiment(ExPhil):

def __new__(cls, *args, **kwargs):
obj = ExPhil.__new__(cls, *args, **kwargs)
obj.exphil_attrs.update({
'type': 'ThoughtExperiment',
})
return obj


# TODO(elimisteve): Create TheoryOfLanguage(Theory), then
# TheoryOfNames(TheoryOfLanguage)?

# TODO(elimisteve): Instead call this TheoryOfReference?
class TheoryOfNames(Theory):

def __new__(cls, *args, **kwargs):
obj = Theory.__new__(cls, *args, **kwargs)
obj.exphil_attrs.update({
'type': 'TheoryOfNames',
})

# There are implicit assumptions made by most theories of names
obj.theory.update({
'names_refer': True,
})

return obj


def __init__(self, *args, **kwargs):
self.theory.update(kwargs)
No changes.

0 comments on commit a576611

Please sign in to comment.
You can’t perform that action at this time.