473,385 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Interface type checking

Hi,
I read a blog entry by GVR on interfaces in which he mentioned that you
had to be able to state the type signature of, say, a function.

That got me thinking along the lines of:
If you have some typical data, then transform it into a string
showing
its sub-types.
Could not a regular expression matching this string be used to check
the type signature of the data?

for example:
data = [[{'a': 1, ('b',):3.0 }, ()]]
stringrep = typeExpand(data)
stringrep 'list<list<dict<string:int,tuple<string>:float>,tu ple<>>>' re.match(r"^list<list<(dict<.*>|tuple<.*>)*>>$",st ringrep) <_sre.SRE_Match object at 0x01611F60>


Looking at the example above, I don't think regular expression matching
is right.
Some extra syntax such as:
typeMatcher.match(r"list<list<(dict<.*>|tuple<.*>) *>>", stringrep
Where this matcher is more like a parser and so does '<' '>' nested
bracket matching; changes . to mean 0 or more types (e.g: 'str'; or
'str,str...'); and allows you the shorthand of writing 'list' for
..list<.*>'.

I've done some work below on typeExpand, but I'm not fluent in a parser
module to implement typeMatcher.match quickly.

Enjoy!
#============== BEGIN typeMatcher.py ==================
'''
Object type Expander
'''

import types
from pprint import pprint as pp

# Map types to a type name
type2name = dict( [ (typ,name[:name.rindex('Type')].lower())
for name,typ in types.__dict__.iteritems()
if type(typ)==types.TypeType
and str(typ).find('<type ')>=0 ]
+[(type(set()), 'set')] )
#pp(type2name)

def typeExpand(obj):
' Expand an objects type'
ty = type(obj)
name = type2name.get(ty,'')
if not name:
# Make up a name. So "<type 'XX'>" becomes "_type_XX_"
name = str(type(obj))
name = name.replace(' ','_')
name = name.replace('<','_')
name = name.replace('>','_')
name = name.replace("'",'')
typeExpansionHandler = globals().get( name+'__TypeHandler', None)
if typeExpansionHandler:
return ''.join([name, '<', typeExpansionHandler(obj), '>'])
else:
return name

def list__TypeHandler(obj):
' How to expand the contents of a list/tuple'
return ','.join([ typeExpand(ob) for ob in obj])
tuple__TypeHandler = list__TypeHandler

def dict__TypeHandler(obj):
' How to expand the contents of a dict'
return ','.join([ '%s:%s' % (typeExpand(name), typeExpand(value))
for name,value in obj.iteritems()])

def match(matchExprString, typeString): pass

#============== END typeMatcher.py ==================

Aug 11 '05 #1
0 1070

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Simon Harvey | last post by:
Festive greetings fellow programmers! I've been programming now for about 4, maybe 5 years now. 4 of those years were at university so and I havent had much work experience of making real world...
8
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various...
4
by: yoz | last post by:
Hi everyone, I wonder if you can shed some light on a problem I have. I am exporting a C# .Net set of classes to COM. All of it is exported properly but the interface inheritences are not in the...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
6
by: wiredless | last post by:
What is the advantage of passing an interface type? according to UML (visio) when i reverse engineer existing code to a UML diagram I get the error "UMLE00046: sample : - An interface cannot...
8
by: Bill Rust | last post by:
I've created an "Add Item" wizard for VB.NET 2003 that allows a user to add a specialized class that works with my application framework. In the wizard, the user can select the interfaces they...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
9
by: hufaunder | last post by:
I have a class "TestSuper" that implements the interface "TestBase". The interface has a property of type "ReturnType". The class "TestSuper" does not return "ReturnType" but a derivation...
11
by: Wojciech Gryc | last post by:
Hi, I recently started using Python and am extremely happy with how productive it's made me, even as a new user. I'm hoping to continue using the language for my research, and have come across a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.